Good day!
I am having trouble displaying a "No records found" message in my PHP process.
Here is the code for my search query:
if(isset($_GET['submit'])) {
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
echo "<table border=1>";
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
echo "</table>";
}
My problem is I don't know where and what to place the conditional statement that will show "No records found".
Hoping that someone would be able to help me on this one.
Thanks in advance.
check if the number of rows in the result is 0 via mysqli_num_rows function and display the message before looping over the result set.
your code could be like
if(isset($_GET['submit'])) {
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
echo "<table border=1>";
// check if results are present
if(mysqli_num_rows($result)>0) {
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
} else {
echo "<tr> <td colspan='2'> No Results found </td></tr>";
}
echo "</table>";
}
You should try this code , if any error any code in you code then you can find that.
$result = mysqli_query($con, $query) or die(mysqli_error($con));
OR
if(!$result){
echo die(mysqli_error($result));
}
The mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.
Aslo from reference:http://php.net/manual/tr/mysqli.affected-rows.php
An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
if(mysqli_affected_rows($con) ==0){ echo "No records found"; }
else{
echo "<table border=1>";
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
echo "</table>";
}
Related
I am trying to display my query results on page. However, whenever I run the code although the query is correct it does not display anything.
Can anyone help? Would be muchly appreciated
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime AS date , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
}
echo "<table border = '1' align='center'>";
echo "<th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
}
?>
replace
echo $row ['date'];
by
echo $row ['Retdatetime'];
To understand query errors you should use mysqli_error() in your code. If there are no errors for executed query, then you can run while loop for it.
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
} else {
echo "<table border = '1' align='center'>";
echo "<tr><th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
echo "</table>";
}
}
?>
fixes given in comments also applied
Please copy/paste the error, given by mysqli_error()
I'm pretty new at PHP/MySQL, so please be patient with me.
I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name];
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching]; <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name]; <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house]; <----This only show the first row of $house under same student, so you need to loop it too.
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
So what you really want to do is
<?php
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
while( $row = mysqli_fetch_array( $select ) ) {
$teaching = $row[teaching];
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
} else {
while( $row2 = mysql_fetch_array($select2) ) {
$student=$row2[student_name];
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
while($row3 = mysql_fetch_array($select3)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>";
}
} // END ELSE
}
} // END ELSE
?>
This one is a bit perplexing since it works perfectly on some records, and not on others. I am trying to pull all records with a specific email and only getting one record in about half of the cases. I have two query sets one pulling the data one is pulling it half of the time.
The first that works all the time is:
$tiera = mysql_query("SELECT SUM(datamb) AS tiera FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tiera1 = mysql_fetch_assoc($tiera);
$tiera2 = $tiera1['tiera'];
$tieradata = mysql_query("SELECT SUM(dataplan) as datatotal FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tieradata1 = mysql_fetch_assoc($tieradata);
$tieradata2 = $tieradata1['datatotal'];
echo "<table id='display1'>
<tr>
<th>Tier:</th>
<th>Total Data in Tier:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
</tr>";
echo "<tr>";
echo "<td>A</td> ";
echo "<td>". $tieradata2 . " MB</td> ";
echo "<td>". $tiera2. " MB</td> ";
echo "<td>".($tieradata1['datatotal'] - ROUND ($tiera1['tiera'],2)) . "MB</td></font> ";
echo "</table>";
I cant use this for my second query because I need it in the loop like this:
$sql = "SELECT phonenumber,date,email, dataplan AS currentplan, SUM(datamb) AS value_sum FROM maindata2 WHERE email='".mysql_real_escape_string($_POST['email'])."' GROUP BY dataplan, phonenumber ";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No Data Found For This Email";
exit;
}
$row = mysql_fetch_assoc($result);
$query = mysql_query($sql) or die(mysql_error());
$header_printed = false;
while($row = mysql_fetch_array($query)) {
if ($row['phonenumber']) {
if ($header_printed === false) {
echo "
<table id='display'>
<tr>
<th>Phone Number:</th>
<th>Data Plan:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
<th>Date Reporting:</th>
</tr>";
$header_printed = true;
}
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "MB</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td><font color=$color>".($row['currentplan'] - ROUND ($row["value_sum"],2)) . "MB</td></font> ";
echo "<td>".$row['date'] . "</td></tr>";
}
}
echo "</table>";
So the question is, what is missing from the second query that is in the first?
I basically have an input system where people enter data and the data is printed in a specific order in a HTML table.
I have some code which works fine below except that every time the row is updated, the table is edited instead adding a new table with the data. Also when i refresh the data disappears?
My code is below:
$query = "SELECT * FROM rumours";
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$band = $row['band'];
$title = $row['Title'];
$description = $row['description'];
}
$sql="INSERT INTO rumours (band, Title, description)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
if (mysql_query($sql, $connect)) {
echo "<table border='1'>";
echo "<tr>";
echo "<td> $title </td>";
echo "</tr>";
echo "<tr>";
echo "<td class = 'td1'> $description </td>";
echo "</tr>";
echo "</table>";
}
echo "1 record added";
mysql_close($connect);
You are only generating the table from the inserted db rows, not all of the db rows in the db table. To do that, you have to echo the table code for each of the found rows as well:
$query = "SELECT * FROM rumours";
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$band = $row['band'];
$title = $row['Title'];
$description = $row['description'];
echo "<table border='1'>";
echo "<tr>";
echo "<td> $title </td>";
echo "</tr>";
echo "<tr>";
echo "<td class = 'td1'> $description </td>";
echo "</tr>";
echo "</table>";
}
$sql="INSERT INTO rumours (band, Title, description)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
/* ... Code truncated to save space */
EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.