简体   繁体   English

无法找到PHP为MySQL错误号定义

[英]Unable to locate PHP defines for MySQL error numbers

In my HTML code there's a select with options and a button that opens a dialog to allow the user to add to the options list [via DB/PHP/jQuery]. 在我的HTML代码中,有一个带有选项的选择和一个打开对话框的按钮,该对话框允许用户[通过DB / PHP / jQuery]添加到选项列表中。

In the case where they enter an option that already exists in the DB, I want to catch the MySQL Duplicate Key error and use it to trigger code that finds the Option that matches what the User entered, and selects it for them. 在他们输入数据库中已经存在的选项的情况下,我想捕获MySQL Duplicate Key错误并使用它来触发代码,该代码找到与用户输入内容匹配的Option,并为他们选择。

mysqli::$errno returns 1062 , and my code works great when I use the raw number. mysqli::$errno returns 1062 ,当我使用原始数字时,我的代码工作得很好。

But what if that number changes in some future update? 但是,如果该数字在将来的更新中发生变化,该怎么办? I would rather use a define provided by the PHP installation. 我宁愿使用PHP安装提供的定义。

The PHP Manuel hints at such a define: PHP Manuel暗示了这样的定义:

Client error message numbers are listed in the MySQL errmsg.h header file, server error message numbers are listed in mysqld_error.h. 客户端错误消息编号在MySQL errmsg.h头文件中列出,服务器错误消息编号在mysqld_error.h中列出。 In the MySQL source distribution you can find a complete list of error messages and error numbers in the file Docs/mysqld_error.txt. 在MySQL源代码发行版中,您可以在文件Docs / mysqld_error.txt中找到错误消息和错误编号的完整列表。

I found the 'mysqld_error.txt' document which has the following define: 我找到了“ mysqld_error.txt”文档,该文档具有以下定义:

#define ER_DUP_ENTRY 1062

There is also the following document: http://dev.mysql.com/doc/refman/5.6/en/error-messages-server.html which has the same define. 还有以下文档: http : //dev.mysql.com/doc/refman/5.6/en/error-messages-server.html具有相同的定义。

But, when I try using this in my PHP code I get a " Use of undefined constant ER_DUP_ENTRY - assumed 'ER_DUP_ENTRY' " notice. 但是,当我尝试在PHP代码中使用此命令时,会收到“ 使用未定义常量ER_DUP_ENTRY-假定为'ER_DUP_ENTRY'的通知。 I even tried mysqli::ER_DUP_ENTRY . 我什至尝试了mysqli :: ER_DUP_ENTRY

Anyone knows a way to do this without using the numeric value? 任何人都知道不使用数字值的方法吗?

All I had to do was to enclose the constant name in quotes. 我要做的就是将常量名称括在引号中。 In my case it works perfectly. 就我而言,它运作完美。

if ('ER_DUP_ENTRY') {
    #logic here;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM