php 如果 mysql_query() 失败,该怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2414569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if mysql_query() fails, what to do?
提问by Wohell
Sometimes so happens that mysql_query() fails to INSERT data and I am unaware of it. So, the question is how do I know when it happens?
有时会发生 mysql_query() 无法插入数据而我不知道它。所以,问题是我怎么知道它什么时候发生?
回答by Pascal MARTIN
Quoting the documentation page of mysql_query:
引用文档页面mysql_query:
For
SELECT,SHOW,DESCRIBE,EXPLAINand other statements returning resultset,mysql_query()returns aresourceon success, orFALSEon error.For other type of SQL statements,
INSERT,UPDATE,DELETE,DROP, etc,mysql_query()returnsTRUEon success orFALSEon error.
对于
SELECT,SHOW,DESCRIBE,EXPLAIN等语句返回的结果集,mysql_query()返回resource成功,或FALSE在错误。对于其他类型的SQL语句,
INSERT,UPDATE,DELETE,DROP,等,mysql_query()收益TRUE上的成功或FALSE出错。
So, to detect whether there's been an error or not, you have to test the return value of your call to mysql_query:
因此,要检测是否有错误,您必须测试调用的返回值mysql_query:
$result = mysql_query('...');
if ($result === false) {
// And error has occured while executing
// the SQL query
}
Then, what can you do ?
那么,你能做什么?
- Well, first, you can log the error to a file, that you can analyse later
- For than, you can use
mysql_errorandmysql_errno.
- For than, you can use
- And you can display a nice error message the user
- i.e. some kind of "oops, an error occured page".
- 好吧,首先,您可以将错误记录到一个文件中,以便您稍后进行分析
- 对于 than,您可以使用
mysql_errorandmysql_errno。
- 对于 than,您可以使用
- 你可以向用户显示一个很好的错误信息
- 即某种“糟糕,页面发生错误”。
Remember, though : the user doesn't need to know (and will not understand)the technical error message -- so don't display it.
但请记住:用户不需要知道(也不会理解)技术错误消息——所以不要显示它。
回答by Daniel Luca CleanUnicorn
One check that I usually do is like in the example
我通常做的一项检查就像在示例中
$result = mysql_query( $query );
if ( !empty( $error = mysql_error() ) )
{
echo 'Mysql error '. $error ."<br />\n";
}
else
{
// the query ran successfully
}
This is a good check for any kind of queries
这是一个很好的检查任何类型的查询
回答by Sean Fisher
You can use mysql_error php.net/mysql_errorto get a better explanation that you then either display or log it in a file.
您可以使用 mysql_error php.net/mysql_error获得更好的解释,然后您可以将其显示或记录到文件中。
回答by SteelBytes
suggest making a wrapper for mysql_query that detects failure and logs the query plus mysql_error somewhere
建议为 mysql_query 制作一个包装器,用于检测失败并在某处记录查询和 mysql_error
回答by MarkR
If you're using a sensible client library, it will throw an exception when a SQL command fails, and you can examine the error code programmatically to know what to do.
如果您使用的是合理的客户端库,它会在 SQL 命令失败时抛出异常,您可以通过编程方式检查错误代码以了解该怎么做。
If you have no code handling that specific error path, you should probably run a standard error handler which logs the error, stack trace and some other debug info into a log.
如果您没有处理该特定错误路径的代码,您可能应该运行一个标准错误处理程序,将错误、堆栈跟踪和其他一些调试信息记录到日志中。
回答by Martin
If the cause of the error is deadlock, you might want to try again.
如果错误的原因是死锁,您可能需要再试一次。
回答by Johan
You can check how many rows MySQL has inserted/updated/deleted by doing this query right after your insert/update/delete query.
您可以通过在插入/更新/删除查询之后立即执行此查询来检查 MySQL 插入/更新/删除了多少行。
SELECT ROW_COUNT() as rows_affected
If this returns 0your insert failed. If it returns 1or more you've succeeded.
如果这返回0您的插入失败。如果它返回1或更多,你就成功了。
See: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
请参阅:http: //dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
回答by Phil Rykoff
you have to analyze your sql. did you escape your parameters? sounds like this could be the problem. you may paste your sql so we can help you.
你必须分析你的sql。你有没有逃脱你的参数?听起来这可能是问题所在。您可以粘贴您的 sql,以便我们可以帮助您。
for showing the error, you can eg.
为了显示错误,您可以例如。
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
.
.
回答by Sejanus
Probably you can check LAST_INSERT_ID() (mysql_insert_id() in PHP). Provided that your table has auto_increment column.
也许您可以检查 LAST_INSERT_ID() (PHP 中的 mysql_insert_id())。前提是您的表具有 auto_increment 列。

