I'm dealing with a MySQL table that defines the JobName column as UNIQUE. If somebody tries to save a new Job to the database using a JobName that is already in the database, MySQL throws a warning.
I would like to be able to detect this warning, just like an error, in my PHP script and deal with it appropriately. Ideally I would like to know what kind of warning MySQL has thrown so that I can branch the code to handle it.
Is this possible? If not, is it because MySQL doesn't have this ability, PHP doesn't have this ability, or both?
For warnings to be "flagged" to PHP natively would require changes to the mysql/mysqli driver, which is obviously beyond the scope of this question. Instead you're going to have to basically check every query you make on the database for warnings:
Obviously this is going to be hideously expensive to apply en-mass, so you might need to carefully think about when and how warnings may arise (which may lead you to refactor to eliminate them).
For reference, MySQL SHOW WARNINGS
Of course, you could dispense with the initial query for the
SELECT @@warning_count
, which would save you a query per execution, but I included it for pedantic completeness.It is possible to get the warnings, and in a more efficient way with mysqli than with mysql.
Here is the code suggested on the manual page on php.net for the property mysqli->warning_count:
Updated to remove the stuff about errno functions which I now realize don't apply in your situation...
One thing in MySQL to be wary of for
UPDATE
statements:mysqli_affected_rows()
will return zero even if theWHERE
clause matched rows, but theSET
clause didn't actually change the data values. I only mention this because that behaviour caused a bug in a system I once looked at--the programmer used that return value to check for errors after an update, assuming a zero meant that some error had occurred. It just meant that the user didn't change any existing values before clicking the update button.So I guess using
mysqli_affected_rows()
can't be relied upon to find such warnings either, unless you have something like anupdate_time
column in your table that will always be assigned a new timestamp value when updated. That sort of workaround seems kinda kludgey though.may be what you are looking for.
The PHP errors can then be handled with a custom PHP error handler, but you can also just turn off displaying php errors as they are usually logged into a log file (depends on your php configuration).
Note on suppressing warnings: Generally, it is not a good idea to prevent warnings from being displayed since you might be missing something important. If you absolutely must hide warnings for some reason, you can do it on an individual basis by placing an
@
sign in front of the statement. That way you don't have to turn off all warning reporting and can limit it to a specific instance.Example:
First, you should turn warnings off so that your visitors don't see your MySQL errors. Second, when you call
mysql_query()
, you should check to see if it returned false. If it did, callmysql_errno()
to find out what went wrong. Match the number returned to the error codes on this page.It looks like this is the error number you're looking for: