Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confused on mysql_query

Status
Not open for further replies.

piperent

Programmer
Feb 7, 2002
156
US
I execute a mysql_query searching for a 'specific' record. The record is not found (I purposely choose an invalid ID) yet the query returns a result. What does this result point to, and why is there any result returned?

ex:
$custid = '250';
$sql_select = "select * from cust_detail where cust_detail.custid = \"$custid\" " ;

$result = mysql_query($sql_select) or die("failed: . mysql_error()");

Should this not take the 'failed' route?

When executed it returns a result set of "Resource ID #4". But any subsequent mysql_fetch_array($result) returns nothing. So what's up with that? If you can't rely on a failed query to 'fail', then how would you ever know?

I'm fairly new to mysql and php and I certainly have only a minimal understanding of the concepts. Maybe someone could clue me in on what to expect, and how to handle this type of occurrence. All the manuals I've read really don't expound on this situation.

Any clarification would be greatly appreciated.

JP
 
mysql_query did not fail. It was successful in querying the database and returning all matching rows. In this case, there happened to be 0 matching rows.

An example where a mysql_query would fail is if you tried querying a nonexistent table, or if you had invalid syntax.

Try
Code:
$sql_select = "select * from cust_detail wher cust_detail.custid = \"$custid\" " ;

and see what you get. The misspelled wher should cause the query to fail.
 
Thanks for the fast response. You have helped in more ways than you might think. My guess has it that a 'mysql_query()' should probably be followed by mysql_num_rows() to check for a valid record. I'll change things up and give it a try.

Thanks again for the clarification. It is much appreciated.

JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top