How to check if query returned empty

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

How to check if query returned empty

Post by Shendemiar »

$kysely1="SELECT * FROM players_games where gid=$GameID and pid=$value";

$haku1 = mysql_query($kysely1, $yhteys) or die("Virhe kyselyssä!");

How do i check if the query returns nothing
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

empty() :-). It checks weather a string is empty or not, not empty it :P.

Sample:

Code: Select all

$name = '';
if(!empty($name)) {
   print "oo, i have a name!";
} else {
   print "where's your name?!?";
}
-Nay
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Thanks Image

It's late in Singapura? (Is it Singapore?)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

<?
if (!empty($name)))
{
if($name==1) { echo " $....."; }
if..

................
...........ect
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To check if a query has returned no rows, count the rows returned, empty()'s not really appropriate for this job but mysql_num_rows() is:

Code: Select all

$kysely1="SELECT * FROM players_games where gid=$GameID and pid=$value";

$haku1 = mysql_query($kysely1, $yhteys) or die("Virhe kyselyssä!"); 

if (mysql_num_rows($haku1) > 0) {
    echo 'rows were returned';
} else {
    echo 'no information returned by the query';
}
Mac
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Better than mysql_num_rows -> SELECT COUNT(*) as count, ...

To test if there are rows in the resultset, just make use of a conditional clause , fe:

if ($row = mysql_fetch_assoc($result)) {
// there is at least one row
} else {
// no such rows
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

timvw wrote:Better than mysql_num_rows -> SELECT COUNT(*) as count, ...
Only if you don't need to do anything else with the select statement except work out how may rows there are going to be returned.
timvw wrote:To test if there are rows in the resultset, just make use of a conditional clause , fe:

if ($row = mysql_fetch_assoc($result)) {
// there is at least one row
} else {
// no such rows
}
That will cause you problems if you are returning more than one record from the database because the conditional statement extracts the first row and you'll have to reset the pointer of the resource before looping through and printing out the records.

mysql_num_rows() is designed to tell you how many rows have been returned, I'm not sure why you need to avoid using it?

Mac
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

twigletmac wrote:
timvw wrote:Better than mysql_num_rows -> SELECT COUNT(*) as count, ...
Only if you don't need to do anything else with the select statement except work out how may rows there are going to be returned.
You're right.
twigletmac wrote:
timvw wrote:To test if there are rows in the resultset, just make use of a conditional clause , fe:

if ($row = mysql_fetch_assoc($result)) {
// there is at least one row
} else {
// no such rows
}
That will cause you problems if you are returning more than one record from the database because the conditional statement extracts the first row and you'll have to reset the pointer of the resource before looping through and printing out the records.
There would be no need to reset that pointer ;)

if ($row = mysql_fetch_assoc($result)) $got_row = true;
while ($got_row) {
// handle row data
if ($row = mysql_fetch_assoc($result)) $got_row = true;
}

twigletmac wrote: mysql_num_rows() is designed to tell you how many rows have been returned, I'm not sure why you need to avoid using it?
I don't really like the function because there have been a few bugreports against it.
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

Wouldn't this work just as well...

Code: Select all

<?php
if (mysql_affected_rows () < 1)
print ("No records found for vehicle!\n");
else
{
}

?>
Post Reply