Page 1 of 1
Symbolic Error Codes
Posted: Thu Jul 12, 2007 10:47 am
by cjm.1956
Hi Folks,
Can I represent the MySQL error codes symbolically? For example:
Code: Select all
$result = mysql_query(...)
if ($result == 1146) // Table not found, so I want to add one...
I would like to replace the 1146 with something from an include file that is maintained by MySQL -- something like MySQL_ERROR_TABLE_NOT_FOUND.
Code: Select all
$result = mysql_query(...)
if ($result == MySQL_ERROR_TABLE_NOT_FOUND) // Table not found, so I want to add one...
Are you aware of any way to do this? I've not found many who tests errors -- they pretty much all simply return them. And the one example I found used the integer value rather than a symbolic representation. Any thoughts?
Chris
Fill what's empty, empty what's full and scratch where it itches ...
Life is a journey, not a destination.
Posted: Thu Jul 12, 2007 11:26 am
by Begby
You can manually create the constants using define().
define('ERROR_CODE', 11111)
I don't know of a way to match the errors with mysql automatically.
However, you shouldn't rely on errors to dictate logic flow. In the example you provide, before you do the query you should test if the table exists, then if it doesn't create it.
Posted: Thu Jul 12, 2007 11:29 am
by cjm.1956
Yes. And that is better than hard-coding magic numbes throughout the code -- even if you are disciplined enough to always include a comment.
But ... Shouldn't this already exist? Shouldn't I be able to include a list of these defines already? Maybe from the lads at MySQL. or PHP? Am I looking for something that does not exist or have i simply not found it yet...
Posted: Thu Jul 12, 2007 12:32 pm
by Begby
As far as I know the codes do not exist for PHP. I believe that the codes are directly passed through from the C API. I am sure there is a C include, but since there is no direct MySQL API for PHP (it has its own which uses the C API) then MySQL will not supply an include.
To make a long story short, if you really need this you are going to have to manually define it. Happy typing!
Posted: Thu Jul 12, 2007 12:59 pm
by RobertGonzalez
Can I ask why you want to use a constant string as opposed to the standard error code that comes from MySQL?
Posted: Thu Jul 12, 2007 1:20 pm
by cjm.1956
Thanks for the help.
Can I ask why you want to use a constant string as opposed to the standard error code that comes from MySQL?
Sure. I have a few reasons.
First, it is clearer to use a symbolic reference eq: ER_NO_SUCH_TABLE then a number (1146). I know that a comment should mitigate this but there are problems with that -- you must be disciplined to put the comment there and you must be sure that the comment always agrees with the code and we have all done a cut and paste leaving invalid comments.
Second, the number 1146 is arbitrary and probably will never change, but it could. If the value of ER_NO_SUCH_TABLE ever changed, it wouldn't matter because I don't know the value, nor do I care.
Third, because this is the way all the other systems I've ever played with work and I thought PHP would (should!) behave this way.
Posted: Thu Jul 12, 2007 1:36 pm
by Begby
Why aren't you just using mysql_error() to get a textual error message?
Posted: Thu Jul 12, 2007 1:39 pm
by stereofrog
mysql error codes are here
http://dev.mysql.com/sources/doxygen/my ... ource.html
you can use your text editor's replace command to convert this to php code
Posted: Thu Jul 12, 2007 1:43 pm
by cjm.1956
Why aren't you just using mysql_error() to get a textual error message?
I could. But that is meant for human consumption and there is no guarantee that it won't change. It is also a much bigger deal for the processor to compare two strings then it is for the processor to compare two integers…
Not to change the subject, but rather to express my frustration –
I looked over hundreds of examples this morning and EVERYBODY I could find did stuff like this:
Code: Select all
(($result = mysql_query(…)) or die (…));
Suppose you don’t want to die? You want to do something adaptive – like for example create the table that is not found?
Posted: Thu Jul 12, 2007 1:46 pm
by Begby
Queries should never die, you should never have to depend on an error happening to have your code work correctly. Like I said, in this case you want to check to see if the table exists by asking the database with a query for a list of tables, if the table is not in that list then create it, otherwise do your other query.
Posted: Thu Jul 12, 2007 2:05 pm
by RobertGonzalez
Yeah, I think the only way you are going to get what you want is to roll your own system. Of course, that throws the added overhead of managing those relationships squarely at you (which is why, I would venture to say, the folks at MySQL have probably never considered it).