Thursday, January 17, 2008

Supplied argument is not a valid MySQL result resource

When I was learning PHP-MySQL, this may one of the most common errors I encountered. I know it feels very bad when you enthusiastically run a query and the server return the error "supplied argument is not a valid MySQL result resource". For rechecking the code I realised that the query was not constructed properly.

Incorrect syntax is the most common offender for newbies. Possibly error lies somewhere in:
  1. You did not spell SELECT correctly. While typing rapidly, it is very common to misplace an E somewhere.
  2. You did not include the asterik (*). It Should be "SELECT * FROM table WHERE..."
  3. You declared the variable containing the query wrongly or did not call it properly. Eg: declaring $query to contain the variable and calling mysql_query to process some other variable like mysql_query($qurey).
  4. You did not connect to the server properly. Either you supplied incorrect login details or chose the incorrect database/table.
For this and many alike reasons, it is always astute to use the mysql_error() function.
It will tell where you went wrong without having to spend time searching the internet.

No comments: