This error message is displayed when you have an error in your query which caused it to fail. It will manifest itself when using:
- `mysql_fetch_array`/`mysqli_fetch_array()`
- `mysql_fetch_assoc()`/`mysqli_fetch_assoc()`
- `mysql_num_rows()`/`mysqli_num_rows()`
**Note**: This error does *not* appear if no rows are affected by your query. Only a query with an invalid syntax will generate this error.
**Troubleshooting Steps**
- Make sure you have your development server configured to display all errors. You can do this by placing this at the top of your files or in your config file: [`error_reporting(-1);`](
[To see links please register here]
). If you have any syntax errors this will point them out to you.
- Use [`mysql_error()`](
[To see links please register here]
). `mysql_error()` will report any errors MySQL encountered while performing your query.
Sample usage:
mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);
if (false === $result) {
echo mysql_error();
}
- Run your query from the MySQL command line or a tool like [phpMyAdmin](
[To see links please register here]
). If you have a syntax error in your query this will tell you what it is.
- Make sure your quotes are correct. A missing quote around the query or a value can cause a query to fail.
- Make sure you are escaping your values. Quotes in your query can cause a query to fail (and also leave you open to SQL injections). Use [`mysql_real_escape_string()`][1] to escape your input.
- Make sure you are not mixing `mysqli_*` and `mysql_*` functions. They are not the same thing and cannot be used together. (If you're going to choose one or the other stick with `mysqli_*`. See below for why.)
**Other tips**
`mysql_*` functions should not be used for new code. They are no longer maintained and the community has begun the [deprecation process](
[To see links please register here]
). Instead you should learn about [prepared statements](
[To see links please register here]
) and use either [PDO](
[To see links please register here]
) or [MySQLi](
[To see links please register here]
). If you can't decide, [this article](
[To see links please register here]
) will help to choose. If you care to learn, here is [good PDO tutorial](
[To see links please register here]
).
[1]:
[To see links please register here]