How to check if query returned empty
Moderator: General Moderators
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
How to check if query returned empty
$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
$haku1 = mysql_query($kysely1, $yhteys) or die("Virhe kyselyssä!");
How do i check if the query returns nothing
empty()
. It checks weather a string is empty or not, not empty it
.
Sample:
-Nay
Sample:
Code: Select all
$name = '';
if(!empty($name)) {
print "oo, i have a name!";
} else {
print "where's your name?!?";
}-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
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';
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:Better than mysql_num_rows -> SELECT COUNT(*) as count, ...
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.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
}
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
You're right.twigletmac wrote: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:Better than mysql_num_rows -> SELECT COUNT(*) as count, ...
There would be no need to reset that pointertwigletmac wrote: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.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
}
if ($row = mysql_fetch_assoc($result)) $got_row = true;
while ($got_row) {
// handle row data
if ($row = mysql_fetch_assoc($result)) $got_row = true;
}
I don't really like the function because there have been a few bugreports against it.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?
Wouldn't this work just as well...
Code: Select all
<?php
if (mysql_affected_rows () < 1)
print ("No records found for vehicle!\n");
else
{
}
?>