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
Http Options Request on Azure Websites Fails Due to Cors
Laravel Add a New Column to Existing Table in a Migration
PHP to Store Images in MySQL or Not
Replace String in Text File Using PHP
Return the Portion of a String Before the First Occurrence of a Character in PHP
Fastest Way to Retrieve a <Title> in PHP
How to Write a Recursive Regex That Matches Nested Parentheses
Regex Matching Links Without <A> Tag
Warning: Preg_Match() [Function.Preg-Match]: Unknown Modifier '/'
Best Way to Allow Plugins for a PHP Application
PHP Create and Save a Txt File to Root Directory
Uploading a File in Chunks Using HTML5
Why and How to Use Anonymous Functions in PHP
Magic _Get Getter for Static Properties in PHP
Is It Ever Ok to Store Password in Plain Text in a PHP Variable or PHP Constant