Check If the Query Results Empty Row MySQLi

How do I check if a table is empty or already my query didn't match any results

I guess, that $conn is a PDO connection? In that case, the method $conn->query() returns an object of type PDOStatement. See https://www.php.net/manual/de/class.pdostatement.php

The method does NOT return the result set.

Instead you can use the PDOStatement object, to fetch the results:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetchAll();

if(empty($result))
{ ... }

In case you are using mysqli, the object returned by query() is this: https://www.php.net/manual/en/class.mysqli-result.php

So the code would be:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);

if(empty($result))
{ ... }

Please also note: Your code is highly insecure! You should use prepared statements to prevent sql-injection:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = :currentUrl";
$stmt = $conn->prepare($sql);
$stmt->execute(['currentUrl' => $currentURL]);
$result = $stmt->fetchAll();

if(empty($result))
{ ... }

mysqli returns empty result - how to display

You might want to look up mysqli_num_rows() in the PHP manual. It lets you see how many rows are in a result set generated by the previous query. You can use the row count to determine whether to display results or the "no matching results" message.

How to check if $row['column_name'] is returning empty php mysql

You can use ==0, this will check if it equals to 0:

if ($row['describe']==0) { /* code to do */ }

Or empty(), this will check if it is empty:

if (empty($row['describe'])) { /* code to do */ }

Personally, I would prefer !empty() as this will check if the variable is empty.

Hope this helps, thanks!

Checking if mysqli_query returned any values?

Use mysqli_num_rows to check if any rows were returned or not.

Best way to check if mysql_query returned any results?

if (mysql_num_rows($result)==0) { PERFORM ACTION }

For PHP 5 and 7 and above use mysqli:

if (mysqli_num_rows($result)==0) { PERFORM ACTION }

MySqli check to see if query has results

==1 checks for exactly 1 record.

What you need is

if (mysqli_num_rows($result) >= 1 ) { echo "yahoo it worked! "; }

Check the documentation here

How to check if a MySQL result is empty

You need to check with number of rows. if it return zero it means table contain no rows. try below code.

// Populate headers
$fields = $result->fetch_fields();
foreach ($fields as $field)
printf("<th>%s</th>", $field->name);

printf("</tr>");

// Write to table
if($result->num_rows== 0){
echo "No Result to display";
}else{
while ($myvar = $result->fetch_row()) {

$date = $myvar[0];
$room_ID = $myvar[1];
$description = $myvar[2];
$firstname = $myvar[3];
$lastname = $myvar[4];
$message = $myvar[5];
$period = $myvar[6];

printf("<tr>");
printf("<td>%s</td><td>%s</td>", $date, $room_ID);
printf("<td>%s</td><td>%s</td>", $description, $firstname);
printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period);
printf("</tr>");

}

}



Related Topics



Leave a reply



Submit