1

I am running into a problem, i am trying to just query a table and get some result back, if there is no result(empty) then it just echo it is empty.

The problem is, before the while($row = mysqli_fetch_assoc($result)) echo works with no problem. But when it is inside the while loop, it echos nothing back, not even if(mysqli_num_rows($result)==0)echo 'There is nothing'; or if i do

if(empty($result)){
echo "There is no";

Here is my code below, thanks for your time

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

session_start();
include 'connect.php';


echo "It is working";

$Listingid = "648";


echo "$Listingid";


$result = mysqli_query($con,"SELECT * FROM list WHERE Listingid = '$Listingid'")
    or die(mysqli_error($con));


while ($row = mysqli_fetch_assoc($result)) {
    if(empty($result)) {
        echo "There is no";
    } else {
        echo 'User ID:' . $row['userid'] . '<br>Username:' . $row['username'] . '<br>Useremail:' . $row['useremail'];
    }
}
6
  • Add mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the top of your code for better error reporting Commented Jul 15, 2014 at 5:43
  • I just added it, no error Commented Jul 15, 2014 at 5:45
  • The problem may be your ob_start(); So you start buffering but never send the buffered output (e.g. echo) to the browser. Commented Jul 15, 2014 at 5:45
  • Then update your question. I'd also get rid of the ob_start(); you have no use for it Commented Jul 15, 2014 at 5:45
  • Try to put if(empty($result)) BEFORE the while-loop, because if $result is empty, it won't jump into the while-loop. Commented Jul 15, 2014 at 5:48

3 Answers 3

1

Try this logic

$sql = mysqli_query($con,"select count(*) as count from list WHERE Listingid = '$Listingid'");
while($row = mysqli_fetch_array($sql))
{
    $count = $row['count'];
}

if($count == 0)
{
   echo 'it is empty';
}
else
{
$result=mysqli_query($con,"SELECT * FROM list WHERE Listingid = '$Listingid'");
while($row = mysqli_fetch_array($result))
{
    echo 'User ID:'.$row['userid'].'<br>Username:'.$row['username'].'<br>Useremail:'.$row['useremail'];
}
}
Sign up to request clarification or add additional context in comments.

6 Comments

Just tried this! It is now echoing the it is empty part!
Can you briefly explain why my code didnt work? Or what the difference is
@user3829266 $result is your mysqli_result object from the query. Given a successful query, this will never be empty. What your code did not do was detect zero results
first query will retutn you the occurance of records for Listingid = '$Listingid' condition. If $count = 0 ,it means no records are present for that condition. Else record exists and show record using second sql query.
There is a syntax error on line 13. The semicolon is missing. It isn't recommended to use two querys for this.
|
1

Try this code:

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

session_start();
include 'connect.php';


echo "It is working";

$Listingid = "648";


echo "$Listingid";


$result = mysqli_query($con, "SELECT * FROM list WHERE Listingid = '$Listingid'");

if (mysqli_num_rows($result) == 0) {
    echo "There is no";
} else {
    while ($row = mysqli_fetch_assoc($result)) {
        echo 'User ID:' . $row['userid'] . '<br>Username:' . $row['username'] . '<br>Useremail:' . $row['useremail'];
    }
}
?>

It checks, if there is data before the while-loop, because it won't go through the while-loop if it doesn't find any result with this query.

Comments

-1

have you tried using mysqli_fetch_array(); instead of mysqli_fetch_assoc();? mysqli_fetch_array(); fetches all fields while mysqli_fetch_assoc(); returns field names with row values, not names ex:

[0] => "Username", [1] =>"Email"

while mysqli_fetch_array(); returns:

[0] => "username", [Username] => "username, [1] => "Email", [Email] => "Email"

and this part:

if(empty($result)) {
    echo "There is no";
}

is quite useless, since if there wasn't a row, the loop wouldn't come at this point of the script.

3 Comments

It should still echo something out tho right? It should still echo out "There is no" It is not even echoing that
Nope, because it doesn't detect the fieldnames "userid" and "username"
Modified my first post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.