I want to check whether my query is empty (has no rows). What is the best way of hecking it?
And what do I get in the following "$result" and "$row" (this is how I am aiming to solve this - if($result)
or maybe using if($row)
means that it is not empty. Any better suggestions?
Thanks.
$result = mysql_query("SELECT * FROM table1 WHERE column1= '$var1'
AND column2 IN ('no','nein','nope')
AND timestamp BETWEEN '$date' AND NOW()");
$row = mysql_fetch_assoc($result);
Test this.
$result = mysql_query("SELECT * FROM table1 WHERE column1= '$var1'
AND column2 IN ('no','nein','nope')
AND timestamp BETWEEN '$date' AND NOW()");
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo $row['column name'];
}
You can use mysql_num_rows($result)
get the number of rows, or you could see if $row === false
;
$row
will be the first row of the recordset OR false
if the query has no results. $result
will be a recordset resource if the query succeeded of false
if it had an error.
If you are this early in the project and/or just learning please consider switching to the new DB interface PDO
. At the least use Mysqli
... the mysql extension youre using now is a bit outdated. It works, and sometimes its even required in legacy apps/sites, but it should be avoided if possible.
Use mysql_num_rows
to get the row count.
$result will always be of type Resource if your query didn't fail.