Rudi
Rudi

Reputation: 11

MySQL SELECT statement doesn't stop when no rows are found

I expect this MySQL query to bomb out when it doesn't find the matching ID in the table:

mysql_query("SELECT * FROM table WHERE id='1' LIMIT 1") OR die("Boom!");

It doesn't. It just goes on processing with no error.

What am I missing here?

Upvotes: 1

Views: 706

Answers (4)

Mike
Mike

Reputation: 1

Run the query from the command line using the mysql command line client to make sure the table is not corrupted.

Upvotes: -1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

A query only "fails" when you make a syntax or logical error in it.

A query returning no rows is still a valid query. Check the resultset that it returns to see how many results you get: in your case, check for there being '0'.

Upvotes: 1

Cosmin
Cosmin

Reputation: 2385

The sql statement does not fail, it actually returns nothing. Look here How does "do something OR DIE()" work in PHP?

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838806

A select statement doesn't fail if it finds no rows - it just returns an empty result set.

You can check mysql_num_rows to see if any rows were found.

Upvotes: 6

Related Questions