Mysql_error() 未捕获 PHP 中的 INSERT 错误
这里是新的,对于编程来说真的很新手,所以放轻松......
我发现我有一个 INSERT
由于重复记录错误而失败。我通过在 MySQL 控制台中使用文字运行查询来解决这个问题,其中弹出了 err#1062。
我想了解的是为什么 mysql_error() 或 mysql_errno() 没有在我的 PHP 脚本中捕获此错误。
下面是我所做的一般设置。我有一个表单提交到一个调用 data_insert()
的 php 文件
function data_insert($var1, $var2, $var3, $var4){
$db = db_connect();
$query = "INSERT INTO exampletable (id, id_2, id_3, id_4)
VALUES ('$var1', '$var2', '$var3', '$var4')";
$result = $db->query($query);
if (!$result)
{
echo ('Database Error:' . mysql_error());
}
else
{
echo "Data added to db";
}
}
数据库连接:
function db_connect()
{
$result = new MySQLi('localhost', 'root', 'root', 'dbname');
if (!$result)
throw new Exception('Could not connect to database server');
else
return $result;
}
我得到的结果是:
Database Error:
PHP 回显“数据库错误:”,因为 INSERT
失败,但没有回显后续的 MySQL 错误信息。老实说,我不太确定我应该看到什么,但是通过阅读其他一些问题,我已经仔细检查了 php.ini 文件的错误处理,并且 E_ALL 和 display_errors 已正确设置(尽管不确定是否在这种情况下很重要)。
我的逻辑中是否有一些我不理解的内容,例如链接资源 mysql_error() 所采用的范围?
感谢您的帮助,我希望这是一件非常明显的事情。
我知道上面缺少 XSS 和安全预防措施以及统一的异常处理。不过婴儿学步。为了方便讨论,这里进行了简化。
new here and really green to programming, so go easy..
I discovered I have an INSERT
that is failing because of a duplicate record error. I figured it out by running the query in a MySQL console with literals, where err#1062 popped up.
What I want to understand is why mysql_error()
or mysql_errno()
didn't catch this error in my PHP script.
Below is a generic setup of what I've done. I have a form that submits to a php file that calls data_insert()
function data_insert($var1, $var2, $var3, $var4){
$db = db_connect();
$query = "INSERT INTO exampletable (id, id_2, id_3, id_4)
VALUES ('$var1', '$var2', '$var3', '$var4')";
$result = $db->query($query);
if (!$result)
{
echo ('Database Error:' . mysql_error());
}
else
{
echo "Data added to db";
}
}
The DB connection:
function db_connect()
{
$result = new MySQLi('localhost', 'root', 'root', 'dbname');
if (!$result)
throw new Exception('Could not connect to database server');
else
return $result;
}
Result I'm getting is:
Database Error:
PHP echos "Database Error:" because the INSERT
fails, but no subsequent MySQL error info is echoed. Honestly, I'm not exactly sure what I'm supposed to see, but through reading some other SO questions, I've double-checked my php.ini file for error handling and E_ALL and display_errors is set appropriately (although not sure if it matters in this case).
Is there something in my logic that I'm not understanding, like the scope of the link resource mysql_error() takes?
Thanks for your help, I'm hoping this is something embarrassingly obvious.
I know the above is missing XSS and security precautions and uniform exception handling. Baby steps though. It's simplified here for discussion's sake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 mysqli(注意 i)进行数据库操作,但正在调用 mysql_error(没有 i)。它们是两个完全不同的接口,并且根本不共享内部状态。一个的数据库句柄/结果在另一个中不可用。
尝试使用
mysqli_error()
(注意I)。You're using mysqli (note the i) for your DB operations, but are calling mysql_error (no i). They're two completely different interfaces, and do not share internal states at at all. DB handles/results from one are not usable in the other.
Try
mysqli_error()
instead (note the I).据我所知,您似乎正在使用 MySQLi 类进行连接和查询,但您正在尝试访问 MySQL 错误消息。 MySQLi 和 MySQL 不一样,因此其中一个的错误不会在另一个中显示。您应该查找 MySQLi 的错误处理,而不是 MySQL。
As far as I can tell, you appear to be using the MySQLi class for connecting and queries, but you're trying to access MySQL error message. MySQLi and MySQL aren't the same, so errors in one will not show in the other. You should look up error handling for MySQLi, not MySQL.
您混淆了连接到 mySQL 数据库的两种单独的方法。
mysql_error()
仅适用于通过mysql_query()
运行的查询。当您使用mysqli时,您必须使用
mysqli_error()
You are confusing two seperate methods for connecting to a mySQL DB.
mysql_error()
will only work on queries that are run throughmysql_query()
.As you are using mysqli, you must use
mysqli_error()