1

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?

random
  • 9,774
  • 10
  • 66
  • 83
Rudi
  • 11
  • 1

4 Answers4

6

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

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

Community
  • 1
  • 1
Cosmin
  • 2,365
  • 2
  • 23
  • 29
1

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'.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-1

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

Chris Snow
  • 23,813
  • 35
  • 144
  • 309
Mike
  • 1
  • This has nothing to do with the question. He's not wondering why no rows are returned, he's expecting `mysql_query` to return `false` in this case. But it's not supposed to, it only returns false when there's an error (e.g. a syntax error in the query or a problem connecting to the server). – Barmar Nov 17 '12 at 08:16