For some reason, the below is not showing the "No results found" error message when querying the database for a selected range, but is displaying the table header and footer.
$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
<?php
if (!$result) { echo("No results found for the selected view");
} else ?>
<table id="results">
<tr>
<th>Rec#</th>
<th>Date</th>
<th>Time</th>
<th>Reading</th>
</tr>
<?php ;
while($row = mysqli_fetch_array($result))
?>
<tr>
<td><?php echo($row['RecID']);?></td>
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
</tr>
<?php
}
?>
<tr>
<td class="footer" colspan="4">- end of report -</td></tr>
</table>
<?php
mysqli_close($con);
?>
Any assistance would be greatly appreciated as theoretically, this should work... Shouldn't it? :-)
$result is probably a result set but it might be empty. yet !$result will not be true. documentation for mysql_query:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. (source: http://php.net/mysqli_query)
you should check with mysqli_num_rows (or something similar)
you can use this condition to check the number of row in query
$result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
<?php
if (mysqli_num_rows($result) === 0) {
echo("No results found for the selected view");
} else {?>
<table id="results">
<tr>
<th>Rec#</th>
<th>Date</th>
<th>Time</th>
<th>Reading</th>
</tr>
<?php ;
while($row = mysqli_fetch_array($result))
?>
<tr>
<td><?php echo($row['RecID']);?></td>
<td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
<td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
<td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
</tr>
<?php } ?>
<tr>
<td class="footer" colspan="4">- end of report -</td></tr>
</table>
<?php
mysqli_close($con);
?>
Related
I want to store three variable values in an array. These are HomeResult, Draw and AwayResult. Which I want to use in an another php file. In the image u can se the final website. Problem is I can get the names of the teams and also the value of rows but result is not success.
Or is there another way to achieve this goal ?
I am not sure if it is the goed way to use B,E,F and O variable form the include php.
code of final site;
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
<th>Result</th>
<th>B</th>
<th>E</th>
<th>F</th>
<th>O</th>
</tr>
</thead>
<tbody>
<?php
include 'ff.php';
include 'result.php';
foreach ($champs as $champ ) :
?>
<tr>
<td><?php echo $champ['HOME'];?></td>
<td><?php echo $champ['AWAY'];?></td>
<td></td>
<td><?php echo $champ['B'];?></td>
<td><?php echo $champ['E'];?></td>
<td><?php echo $champ['F'];?></td>
<td><?php echo $champ['O'];?></td>
</tr>
<?php
endforeach;
?>
file result.php
include 'champs.php';
$HomeWins=0;
$Draw=0;
$AwayWins=0;
foreach ($champs as $champ ) :
$B =$champ['B'];
$E=$champ['E'];
$F=$champ['F'];
$O=$champ['O'];
$sql = "SELECT * FROM England where B = '$B' AND E = '$E' AND F = '$F' AND O ='$O' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount=mysqli_num_rows($result);
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['Q'] == 1){
$HomeWins++;
}
elseif($row['Q'] == 0){
$Draw++;
}else{
$AwayWins++;
}
}
$HomeResult =round(($HomeWins/$rowcount )*100);
$DrawResult = round(($Draw/$rowcount )*100);
$AwayResult =round( ($AwayWins/$rowcount )*100);
} else {
echo "0 results";
}
endforeach;
$conn->close();
?>
I create a table in which I create just one <tr> and 11 <td>. The values of first 9 <td> come from one while loop and last 2 values of <td> come from second while loop.
Both While loops are executing in <tr>. Now, I get the correct values, but it repeats the <td> as the total number of records e.g I have total 5 records, it gives me 25 records and the first record executes 5 times continuously and rest like that.
My question is to execute the record once, not to repeat the duplicate records.
This is my code:
<table id="myTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Status</th>
<th>Reg on</th>
<th>Upload</th>
<th>Total Upload</th>
<th>Sale</th>
<th>Total Sale</th>
<th>Purchase</th>
<th>Total Purchase</th>
</tr>
</thead>
<tbody>
<?php
include("connection.php");
$query ="SELECT s.*, s.student_id, s.student_email, u.student_email, b.payer_email,
COUNT(u.student_email) AS 'uploadCount',
SUM(u.price) AS 'uploadTotal',
COUNT(b.payer_email) AS 'buyCount',
SUM(b.payment_amount) AS 'buyTotal'
FROM students s
LEFT JOIN academic_work u ON u.student_email = s.student_email
LEFT JOIN orders b ON b.payer_email = s.student_email
GROUP BY s.student_id
ORDER BY s.student_id DESC
";
$run = mysqli_query($con, $query) or die(mysqli_error($con));
?>
<tr>
<?php
while ($row=mysqli_fetch_array($run)) {
$student_id=$row['0'];
$student_first_name=$row[1];
$student_last_name=$row[2];
$student_email=$row[3];
$student_password=$row[4];
$student_status=$row[5];
$student_time=$row[6];
$uploadCount = $row['uploadCount'];
$buyCount = $row['buyCount'];
$uploadTotal = $row['uploadTotal'];
$buyTotal = $row['buyTotal'];
$query1 ="SELECT ss.*, ss.student_id, ss.student_email, p.email,
COUNT(p.email) AS 'purchaseCount',
SUM(p.payment_amount) AS 'purchaseTotal'
FROM students ss
LEFT JOIN orders p ON p.email = ss.student_email
GROUP BY ss.student_id
";
$run1 = mysqli_query($con, $query1) or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($run1)) {
$purchaseCount = $row1['purchaseCount'];
$purchaseTotal = $row1['purchaseTotal'];
?>
<td><?php echo $student_first_name; ?></td>
<td><?php echo $student_last_name; ?></td>
<td><?php echo $student_email; ?></td>
<td><?php echo $student_status; ?></td>
<td><?php echo $student_time; ?></td>
<td><?php if($uploadCount == 0) { echo 0; } else { echo $uploadCount; }?></td>
<td><?php if($uploadTotal == 0) { echo 0; } else { echo $uploadTotal; }?></td>
<td><?php if($buyCount == 0) { echo 0; } else { echo $buyCount; }?></td>
<td><?php if($buyTotal == 0) { echo 0; } else { echo $buyTotal; }?></td>
<td><?php if($purchaseCount == 0) { echo 0; } else { echo $purchaseCount; }?></td>
<td><?php if($purchaseTotal == 0) { echo 0; } else { echo $purchaseTotal; }?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Thanks in advance!
Your $query1 loops through all rows of the table on each iteration. Use a where clause and then it will return the 1 row you want. Use the id of $row to target the correct row. You should parameterize this so you aren't open to a SQL injection (https://security.stackexchange.com/questions/146595/second-order-sql-injection-protection).
This also could probably be done in 1 query, looping a query is usually an incorrect implementation.
I have a foreach loop to show all of the usernames from my database.
When I run the loop I get, '10' is the rank.
Daniel 10
Daniel 10
Daniel 10
Daniel 10
Daniel 10
This is the loop that I have
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users ");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<h1>Players</h1>
<table>
<tr>
<th>Battletag</th>
<th>Preferred Role</th>
<th>Rank</th>
</tr>
</tr>
<?php var_dump($row)?>
<?php foreach($userRow as $row): ?>
<td> <?php print($row['user_name']); ?></td>
<td> <?php print($row['user_rank']); ?></td>
</tr>
<?php endforeach; ?>
Home
</table>
I don't see why it is looping the 1 user 5 times, instead of looping through all users once
OK 3 things. First, you don't need parameter binding in
$stmt->execute(array(":user_id"=>$user_id));
so, instead of that, use:
stmt->execute();
Next, instead of fetch, use fetchAll function. You need this in order to get all rows for your query from database, instead of only first one.
And finally, in the loop the problem is that you are using $userRow instead of $row inside for loop. Try:
<?php foreach($userRow as $row): ?>
<td> <?=$row['user_name']?></td>
<td> <?=$row['user_rank']?></td>
</tr>
<?php endforeach; ?>
I think this might work for you. It's not a for each loop though. If you're set on using a for each loop then disregard.
$sql = "SELECT user_name, user_rank,
FROM XXX//your database//";
$result = $conn->query($sql);
//Display results
if ($result->num_rows > 0) {
echo '<table>
<tr>
<th>User Name</th>
<th>User Rank</th>
</tr>';
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["user_name"]. "</td>
<td>" . $row["user_rank"]. "</td>
</tr>";
}
echo "</table>";
} else {
$message = "0 results";}
$conn->close();
?>
Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.
change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.
you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.
My query returns the correct data when executed on the db, but when run live the company_title comes back null. All other fields work.
$row['company_name'] is the code i can't get to work:
$query = "select * from invoices i, company_lookup cl, students s where i.company_id = cl.company_id and i.student_id = s.student_id;";
$results = $DB->query($query);
$invoices = mysql_query("select * from invoices");
?>
<table border="1" id="hl" name="hl">
<tr>
<th>Month/Year</th>
<th>Full Amount</th>
<th>Company</th>
</tr>
<?php while ($row = mysql_fetch_array($invoices)) {
$invoice_date = $row['invoice_date'];
?>
<tr onMouseOver="showInvoicePayments(<? echo $row['invoice_id'] ?>);this.bgColor = '#C0C0C0'" onMouseOut ="this.bgColor = '#FFFFFF'" bgcolor="#FFFFFF">
<td><? echo date('F Y',strtotime($invoice_date)) ?></td>
<td><? echo '$' . $row['full_amount'] ?></td>
<td><? echo $row['company_name'] ?></td>
</tr>
<?
}
?>
try adding COALESCE in some of your fields.
like this one:
SELECT ..., COALESCE(company_title, ''),
COALESCE(company_name, '')
...
Instead of null value, it will return an empty string.