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 }
How to check if mysql_query returned anything or not
You could use is_resource
to make sure that it's a resource. You could alternatively use an if
statement to make sure that FALSE
wasn't returned.
To display the error, use mysql_error
.
Example:
<?php
// From http://ca2.php.net/manual/en/function.mysql-query.php
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
trigger_error('Invalid query: ' . mysql_error());
}
Checking if mysql_query returned anything or not
You could use mysql_num_rows($results) to check if 0 rows were returned, or use this faster alternative:
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
Check whether mysql query returned an empty resultset
Finally i got the solution, may be its not the smarter way, but my purpose is solved. I changed the query as --
$stmt = $linkID1->prepare("select count(fstatus),fstatus from cae_friends where provider_email=? and request_email=?");
$stmt->bind_param("ss", $uid,$row['email']);
$stmt->execute();
$stmt->bind_result($val,$fstatus);
$stmt->fetch();
echo "Row Count : ".$val;
Now $val is giving out the right output.
Thanks guys
PHP Mysql how to show message when query return no result?
You shouldn't use the mysql_
functions anymore the are deprecated and don't allow you to use parameterized queries which will prevent you from being SQL injected.
Here's a way using the mysql_
functions. You can easily transistion this to mysqli
or pdo
because they both have the num_rows
function just check the manual for their implementation.
$category = mysql_real_escape_string($_POST['category']);
$length = mysql_real_escape_string($_POST['length']);
$con = mysql_connect("localhost","globalex","[?H~hS=Oc=ES");
$db = mysql_select_db("globalex",$con);
$query = "SELECT list FROM answers WHERE category = '$category' AND length = '$length'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) == 0) {
echo "No Results";
} else {
while($row = mysql_fetch_array($result)) {
echo 'Category '. $category .' '. $length .' letters: ';
echo '<b>'. $row['0'].'</b><br>';
}
}
You can read more about SQL injection prevention here, How can I prevent SQL injection in PHP? and more about why the mysql_
functions shouldn't be used here Why shouldn't I use mysql_* functions in PHP?.
PHP & MySQL, what's returned when the query yields no rows?
That would fall into the case of the first part of the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
A query that returns no result rows will return neither true
, nor false
, but a resource object.
However, the resource object will have no rows, i.e., mysql_num_rows()
will return 0
and the first call to mysql_fetch_*
will return FALSE
. There are a number of ways that you can detect this situation, but calling mysql_num_rows()
is probably one of the easiest.
How to check if a MySQL query using the legacy API was successful?
This is the first example in the manual page for mysql_query
:
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
If you wish to use something other than die
, then I'd suggest trigger_error
.
Related Topics
Can File Uploads Time Out in PHP
How to Emulate a Get Request Exactly Like a Web Browser
How to Enable Curl in Wamp Server
Pdo With "Where... In" Queries
How to Change Envelope from Address Using PHP Mail
PHP-Intl Installation on Xampp
Interpolation (Double Quoted String) of Associative Arrays in PHP
How to Sort a Multidimensional Array by One of the Fields of the Inner Array in PHP
Why PHP Script Is Not Workig in a Web Browser
How to Check If a File Is Mp3 or Image File
Get in Woocommerce Cart the Product Id of a Cart Item
Explode String into Array With No Empty Elements
Using PHP'S Swiftmailer With Gmail
How to Make a PHP Form That Submits to Self
How to Change Maximum Number of Post Variable in PHP
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
Converting a Number (1, 2, 3) to a String (One, Two, Three) in PHP