2

I am working on trying to automatically recover from some mysql errors and am wondering if the error codes I get from $mysqli->errno are defined anywhere. I found a list at http://docs.camlcity.org/docs/godisrc/ocaml-mysql-1.0.4.tar.gz/ocaml-mysql-1.0.4/etc/mysqld_error.txt but am wondering if they are actually stored in the mysqli structure somewhere so I do not have to use magic numbers. Thank you very much for any help.

jpevarnek
  • 381
  • 1
  • 8
  • You can get the text of the error in `$mysqli->error` – keithhatfield Mar 28 '12 at 17:28
  • What I am looking to do is automatically detect when an error is of a certain type (specifically the duplicate entry). I can look up what the errno for this is right now (1062) but would prefer to have a constant (something like `$mysqli::ER_DUP_ENTRY`) but cannot find anything in the documentation mentioning that. I am mostly wondering if there is somewhere the error codes are defined that I am not seeing. – jpevarnek Mar 28 '12 at 17:48
  • I'm hoping to see if I can do the same thing. Did you ever find anything? – Stspurg Aug 08 '13 at 20:22
  • Sadly, I was not able to find anything. I ended up just making a file with defines for the ones I cared about and hoping they do not change (which they probably will not). – jpevarnek Aug 27 '13 at 15:21

1 Answers1

0

Since the mysql errors depend heavily on the server version you use, I created a small Bash script which you can run on your server to generate a PHP class containing all error codes.

#!/bin/bash
if [ ! -n "$1" ]
then
    echo "usage: $0 <path to errormsg.txt>";
    exit 1;
fi
echo "class MysqlErrorConst {";
CURRENT_ERROR_CODE=`head $1 | grep "start-error-number" | sed 's/start-error-number //' | tr -d "\n"`
for CURRENT_ERROR_NAME in `grep "^[A-Z_]\+" $1 | sed 's/\(^[^ ]*\) .*/\1/g'`
do
    echo -e "\t const $CURRENT_ERROR_NAME = $CURRENT_ERROR_CODE;\n";
    ((CURRENT_ERROR_CODE++));
done
echo "}";

Save this script and run it, passing the location of your server's errmsg[-utf8].txt. It will generate something like:

class MysqlErrorConst {
    const ER_HASHCHK = 1000;

    const ER_NISAMCHK = 1001;

    const ER_NO = 1002;

    const ER_YES = 1003;

    const ER_CANT_CREATE_FILE = 1004;

[...]
wolfo
  • 36
  • 4