How to avoid such an error
This error is secondary. And in a properly designed application, it should not arise in principle.
It only signals that the previous function that executed the SQL query failed, but does not carry any information about the cause of the failure.
To avoid such errors in the code, it is necessary to check the result of the same previous function that performed the SQL query. But this should be done with the mind, and not in the way that non-specialists advise, for decades rewriting the same code from each other, not understanding its meaning and not interfering with the results of its work ( very deplorable ) in practice.
When calling the mysql_query() function, you should always check the result of its work. And if the function returned not a correct resource, but emptiness, then it is necessary, first, to receive an error message from mysql, and secondly, to translate it into a PHP error (this is a fundamental point that novice PHP users do not understand completely). And thirdly, it is very useful to add the request itself to the error message.
The mysql_error() function is responsible for the first, trigger_error() for the second, and for the third, you must always assign the request to a variable first. Thus, any mysql_query() call should look like this:
$sql = "SELECT ..."; $res = mysql_query($sql) or trigger_error(mysql_error()." in ". $sql);
Thus, when an error occurs in the execution of the request, the PHP user will be immediately informed in the same way as about any other errors occurring in the script. And before the error "expects parameter" it no longer comes.
In principle, even better than trigger_error (), it would throw an exception. But since
throw new Exception() cannot be substituted so pretty via or in the same line;- PHP beginner users are very poorly aware of the exclusion mechanism and immediately begin to use it incorrectly;
- the
mysql extension has already lost all meaning, and the remaining two, mysqli and PDO are able to translate database errors into exceptions automatically ,
suggesting exceptions for mysql_query() is somehow silly. But in any case, no matter how the error is handled, it must follow two immutable rules:
- No
echo and die() !!! Database errors should always be translated to PHP errors and output where all others are output. If the site prohibits the display of errors in the browser, then database errors should not be an exception to this rule. - The error message must contain the name of the file and the line in which the error occurred, and, if possible, a trace of the calls.
How to fix the error.
You must read the error message.
It sounds commonplace, but surprisingly, none of the laypersons who give advice never mentions it! While reading the text of the error helps a hundred times better than shamanic gestures like "recount all quotes":
First, mysql will immediately tell you what the essence of the error is . If there is no table in the database to which we are accessing, or the entire server has completely dropped, then it is useless to recalculate the quotes.
Secondly, if the error is still in the syntax, then mysql will accurately indicate the place where to look for it - it will quote a piece of query that starts right after the error.
Get rid of syntax errors caused by data once and for all.
If the problem is still in syntax, and at the same time caused by the data sent to the request, then the Foolish Idea itself will "screen your values with mysql_real_escape_string() ". And even more stupid will use this feature to protect against SQL injection. It is not intended for this.
In order to permanently get rid of any problems associated with the variables passed to the query, you must stop inserting them directly into the query string. And to do this only through an intermediary , called "placeholder" .
A driver for working with a database through placeholders can be written on the basis of any API - be it mysql, mysqli or PDO. But since, firstly, for this you need to have special knowledge, and secondly, beginner PHP users are terribly afraid of any ready-made libraries, preferring to use only built-in language tools, they have only one choice left - PDO.
Mysqli
To translate database errors into PHP errors, mysqli does not need to check the result of each function. Instead, just write a line like this before the connection:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
and then all database errors will raise exceptions, which by default become fatal PHP errors and in this form will be available to the programmer.