Forum Moderators: coopster

Message Too Old, No Replies

query without result

         

hartzoua

10:46 am on Jun 30, 2004 (gmt 0)

10+ Year Member



Hi,
I have a problem using PHP with MySQL. I hope you can find a solution.
I am creating a search form which searches a database. I have to use two different computers(due to the fact that I am student).
In my computer when a select query has no records returned, it returns a result with no rows

$query="SELECT ........";
$result=musql_query($query);

if (!$result) //it doesn't work
do this;

if (mysql_num_rows($result)==0) //it works
do this;

In the labs of the university however it doesn't return a result at all

if (!$result) //it works
do this;

if (mysql_num_rows($result)==0)
{
/* returns: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource */
do this;
}

Which of these two is the normal?
If it is something that I can change from the .ini file, what exactly do i have to change?

Thanks in advance

ZibingsPrez

1:45 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Well, first of all, you should probably reconsider your coding logic in this one. It looks alright for the most part, except that when the program finds out that the query results are false (ie, mysql_query() returned an error), it shouldn't continue trying to do things with that result anyways:


if (!$result )
{
//do something with error
}

else
{
if ( mysql_num_rows($result) <= 0 )
{
//do something else with this error
}

else
{
//do what you want
}
}

As far as anything with your php.ini file goes, wouldn't worry about changing anything if you change your program logic like my example above.

hartzoua

4:04 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Thank you ZibingsPrez,
with the code you sent me i can handle all the cases. But, the problem still exists. In the one computer even if the query is absolutely correct but without result the executable code will be in the
if(!$result) case.
In the other computer, for the same query the executable code will be in the
else
{
if(mysql_num_rows($result)<=0) case.

It works with your way but it seems that I will have to write the same things two times. It's weird!
Any other opinion?

httpwebwitch

4:45 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The difference between your home system and the university is not the PHP, it's the way mySQL is set up; Look more closely at your SELECT queries and compromise so it works in both places. Maybe you're using some weird avant-garde SQL that the university database doesn't understand?

if (!$result){
// um, your SQL is buggered up, or the
// database has crashed? Or maybe your
// connection is using the wrong password?
// $result should never look like this

}elseif (!mysql_num_rows($result)){
// $result is empty; no rows returned.
// this is an acceptable empty set.
// your SQL command is OK!

}else{
// you have data!

}

if you get this:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

That means your query returned nothing, and the problem occured while processing the database request. I often see:
1) no connection made to the database
2) database is not available
3) SQL query has bad syntax
4) SQL query uses a JOIN or GROUP or other
command that isn't supported
5) typo

IMHO, whenever you request *anything* from the database, you should always get *something* back. At least an empty set. If you get an empty set, that's OK, you can still do a mysql_num_rows() on an empty set. And if you get a bunch of rows, that's good. if you get NOTHING, that's bad. Unfortunately you often don't find out until you try to do a mysql_num_rows() on the result.

good luck

hartzoua

7:41 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Thanks httpwebwitch,
the analysis was more clear than i could expected.
Finally, I think I can live with this weirdness!