Page 1 of 1

no result from querry but $result still not empty

Posted: Sat Jul 17, 2010 7:18 pm
by iansane
Hi,

I'm working on a php form that queries mysql. When I use

Code: Select all

$result= mysql_query($sql);
Even with bad input that returns no result it acts as if there is still something in $result.

Code: Select all

if(!isset($result)){
echo "nothing found";
}
Doesn't work. Nothing is echoed.

Code: Select all

echo $result;
echoes "resource id #4"


Should I stick with using my $row count for the condition in "if" and "case" statements?

I thought the above "if(!isset($result))" is the right way.

Thanks

Re: no result from querry but $result still not empty

Posted: Sat Jul 17, 2010 9:29 pm
by Gargoyle
RTM

mysql_query will return the resource id of the mysql connection, which then needs to be handed to over to another function (i.e. mysql_fetch_assoc()) to actually do something with it.

Code: Select all

$result = mysql_query($sql);
if(!$result)
{die('invalid mysql query');}
else
{$data = mysql_fetch_assoc($result);}

Re: no result from querry but $result still not empty

Posted: Sun Jul 18, 2010 4:39 pm
by liljester
mysql_query() will return false if the query FAILS, aka has a sql error. youre checking for isset($result). $result will ALWAYS be set when you use mysql_query... if it fails it IS SET to false. you should check with mysql_num_rows to determine if you have any results.

Re: no result from querry but $result still not empty

Posted: Sun Jul 18, 2010 8:44 pm
by iansane
Thanks for the replies.

I am using count from msql_num_rows() and that works. I just thought I had seen result from mysql_query() used in the same way but I guess not since even when num_rows() = 0 the result is still set. I wasn't aware that it was set to false if nothing was returned from mysql. Thanks for the info.