I want to check for no result in MySQL query and return a no result message to the user. How do I integrate it with my current code?
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
echo '<div class="mydiv">';
if($row['photo']){
echo "<p>" . '<span class="glyphicon" aria-hidden="true"></span>' . '</p>' . '<p>' . "<img src=http://localhost:8888/example/images/" . $row['photo'] . "><hr>";
}else
echo '<p>' . '<span class="glyphicon" aria-hidden="true"></span>' . $row['message'] . '</p>';
echo '<p>' . '~' . $row['username'] . '</p>';
echo '<p>' . $row['datetime'] . ' ' .'(UTC)' . '</p>';
echo '</div>';
}
I want a "no result" message to show up when there are no records found. How do I integrate it in my existing code. Please advise.
if ($row = mysql_num_rows($result) == 0){
echo "<h3>There are no result found.</h3>";
}
if ($result = mysql_query($sql) && mysql_num_rows($result) > 0) {
// there are results in $result
// whatever you want to do with your results
} else {
// no results
echo "<h3>There are no results found.</h3>";
}
Related
I am using php and mysql to display all the user information of different users and i have a button which gets the id which will be redirected to another page and the id will be displayed in the url. What i am trying to do now is display the user information with the selected id on the redirected page
$sql = "SELECT * FROM users";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<div class='users-data'>";
echo "<p>" . $row['username'] . "</p>";
echo "<p>" . $row['full_name'] . "</p>";
echo "<p>" . $row['age'] . "</p>";
echo "<p>" . $row['gender'] . "</p>";
echo "<p>" . $row['email'] . "</p>";
echo "<p>" . $row['medical_condition'] . "</p>";
echo "<img src=images/".$row['image'] ."/>";
echo '<td><a href="view-user-information.php?id='.$row['id'].'"><button>View Details</button></td>';
echo "</div>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Use this code;
if(isset($_GET['u'])){
$id = $_GET['u'];
$sql = "SELECT * FROM `users` WHERE `id` = '$id'";
$result = mysqli_query ($link,$sql);
$row = mysqli_fetch_assoc($result);
echo "<div class='users-data'>";
echo "<p>" . $row['username'] . "</p>";
echo "<p>" . $row['full_name'] . "</p>";
echo "<p>" . $row['age'] . "</p>";
echo "<p>" . $row['gender'] . "</p>";
echo "<p>" . $row['email'] . "</p>";
echo "<p>" . $row['medical_condition'] . "</p>";
echo "<img src=images/".$row['image'] ."/>"
}
else
{
//you can redirect it to back to the previous page if no id exist in url;
header("LOCATION:index.php"); // change the index.php to your privious page url
}
I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}
I am trying to implement a dropdown search option. All my search results are working. All the commands that I have assigned to if statements work, but when it does to else it deosn't work.
Here is my code:
if(isset($_REQUEST['submit'])){
$opt = $_POST['opt'];
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else{
echo "Your Searched keyword did not match";
}
}
What to do?
Try this: Take a flag to check if record exists.
$flag = false;
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}
if(!$flag){
echo "Your Searched keyword did not match";
}
I have this script here that shows a list of results. How to do l say "No results" if no results are found. I believe it's the else statement but couldn't quiet get it to work.
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><img src="../../Images/Icons/table-delete.png"/></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
?>
You can try with mysql_num_rows function:
$count = mysql_num_rows($result);
if ($count > 0) {
// loop rows
} else {
// no result
}
Put your while loop in a if-statement and check if there is any results before running the loop. Then you echo "No results" in the else.
It should be like this:
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
if( mysql_num_rows($result)) {
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img
src="../../Images/Icons/table-delete.png"/></a></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
}
else {
echo "No Result";
}
?>
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}