Sometimes so happens that mysql_query() fails to INSERT data and I am unaware of it. So, the question is how do I know when it happens?
Return Values ¶ For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or false on error. For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns true on success or false on error.
mysql_query() sends a query to the currently active database on the server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mysql_connect() was called with no arguments, and use it.
PHP uses mysqli query() or mysql_query() function to insert a record into a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.
Description ¶ Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.
Quoting the documentation page of mysql_query
:
For
SELECT
,SHOW
,DESCRIBE
,EXPLAIN
and other statements returning resultset,mysql_query()
returns aresource
on success, orFALSE
on error.For other type of SQL statements,
INSERT
,UPDATE
,DELETE
,DROP
, etc,mysql_query()
returnsTRUE
on success orFALSE
on error.
So, to detect whether there's been an error or not, you have to test the return value of your call to mysql_query
:
$result = mysql_query('...');
if ($result === false) {
// And error has occured while executing
// the SQL query
}
Then, what can you do ?
mysql_error
and mysql_errno
.Remember, though : the user doesn't need to know (and will not understand) the technical error message -- so don't display it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With