php 如何调试为什么最简单的 MySQL 查询返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9619610/
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
How can I debug why simplest MySQL query returns false?
提问by tatiana_c
I work with xampp. I performed MySQL connection:
我和 xampp 一起工作。我执行了 MySQL 连接:
$connection = mysql_connect($host , $user , $passw);
mysql_select_db($db, $connection);
I received output with echo command (by check the boolean returned values) that connection is established and the database $db is found.
我收到了带有 echo 命令的输出(通过检查布尔返回值),该连接已建立并且数据库 $db 已找到。
But the simplest query like:
但最简单的查询如:
$query = mysql_query("SELECT * FROM 'users'");
returns false. How can I debug why?Thanks.
返回假。我怎样才能调试为什么?谢谢。
回答by Your Common Sense
An obligatory update: as mysql ext is no more, here are answers for two remaining MySQL APIs which I written on my site based on the experience from answering 1000s questions on Stack Overflow:
强制性更新:由于 mysql ext 已不复存在,以下是我根据在 Stack Overflow 上回答 1000 多个问题的经验在我的网站上编写的其余两个 MySQL API 的答案:
- How to report errors in mysqli
- How to connect to MySQL using PDO(with the aim of the proper error reporting).
- 如何在mysqli中报告错误
- 如何使用 PDO 连接到 MySQL(目的是正确报告错误)。
In short, for mysqi the following line have to be added before mysqli_connect()
call:
简而言之,对于 mysqi,必须在mysqli_connect()
调用前添加以下行:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
while for PDO the proper error mode have to be set, for example
而对于 PDO,必须设置正确的错误模式,例如
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
As of the old mysql ext,
从旧的 mysql ext 开始,
To get an error from mysql_query()
you have to use mysql_error()
function.
So always run all your queries this way, at least until you develop a more advanced query handler:
要从mysql_query()
你那里得到错误,你必须使用mysql_error()
函数。
因此,始终以这种方式运行所有查询,至少在您开发更高级的查询处理程序之前:
$query = "SELECT * FROM 'users'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
the problem with your current query is 'users'
part. Single quotes have to be used to delimit strings while for the identifiers you have to use backticks:
您当前查询的问题是'users'
一部分。必须使用单引号来分隔字符串,而对于标识符,您必须使用反引号:
SELECT * FROM `users`
In order to see these errors during development, add these lines at the top of your code to be sure you can see every error occurred
为了在开发过程中看到这些错误,请在代码顶部添加这些行,以确保您可以看到发生的每个错误
ini_set('display_errors',1);
error_reporting(E_ALL);
on the production server, however, the value on the first line should be changed from 1 to 0
但是,在生产服务器上,第一行的值应该从 1 更改为 0
回答by Ynhockey
Use the mysql_error() function:
使用 mysql_error() 函数:
$query = mysql_query("SELECT * FROM 'users'") or die(mysql_error());
EDIT: Per Col. Shrapnel's comment: you should never use die() outside of a test environment. In general it's bad practice when writing code that's even intended for production.
编辑:Per Col. Shrapnel 的评论:你不应该在测试环境之外使用 die()。通常,在编写甚至用于生产的代码时,这是不好的做法。
Here is some more information: http://www.phpfreaks.com/blog/or-die-must-die
回答by mrroot5
Based on Your Common Senseanswer this is an object oriented style:
根据您的常识答案,这是一种面向对象的风格:
$query = "SELECT * FROM 'users'";
$result = $mysqli -> query($query) or trigger_error($mysqli -> error." ".$query);