Page 1 of 1
How to check if query returned empty
Posted: Mon Jan 19, 2004 11:43 am
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
Posted: Mon Jan 19, 2004 11:50 am
by Nay
empty()

. It checks weather a string is empty or not, not empty it

.
Sample:
Code: Select all
$name = '';
if(!empty($name)) {
print "oo, i have a name!";
} else {
print "where's your name?!?";
}
-Nay
Posted: Mon Jan 19, 2004 11:55 am
by Shendemiar
Thanks
It's late in Singapura? (Is it Singapore?)
Posted: Mon Jan 19, 2004 1:02 pm
by ol4pr0
<?
if (!empty($name)))
{
if($name==1) { echo " $....."; }
if..
................
...........ect
Posted: Tue Jan 20, 2004 4:21 am
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
Posted: Tue Jan 20, 2004 7:17 am
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
}
Posted: Tue Jan 20, 2004 8:13 am
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
Posted: Tue Jan 20, 2004 11:16 am
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.
Posted: Tue Jan 20, 2004 11:33 am
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
{
}
?>