如何调试为什么最简单的MySQL查询返回false? [英] How can I debug why simplest MySQL query returns false?

查看:153
本文介绍了如何调试为什么最简单的MySQL查询返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与xampp合作.我执行了MySQL连接:

I work with xampp. I performed MySQL connection:

$connection = mysql_connect($host , $user , $passw);
mysql_select_db($db, $connection);

我通过echo命令(通过检查布尔返回值)接收到输出,该连接已建立并且已找到数据库$ db.

I received output with echo command (by check the boolean returned values) that connection is established and the database $db is found.

但最简单的查询如下:

$query = mysql_query("SELECT * FROM 'users'");

返回假.我该如何调试原因?

returns false. How can I debug why?Thanks.

推荐答案

强制性更新:由于mysql ext不再存在,以下是我在我的网站上基于剩下的两个MySQL API的答案回答关于堆栈溢出的上千个问题的经验:

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:

  • How to report errors in mysqli
  • How to connect to MySQL using PDO (with the aim of the proper error reporting).

简而言之,对于mysqi,必须在mysqli_connect()调用之前添加以下行:

In short, for mysqi the following line have to be added before mysqli_connect() call:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

例如,对于PDO,必须设置正确的错误模式

while for PDO the proper error mode have to be set, for example

$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

从旧版mysql ext开始,

As of the old mysql ext,

要从mysql_query()中获取错误,必须使用mysql_error()函数.
因此,请始终以这种方式运行所有查询,至少直到开发出更高级的查询处理程序为止:

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:

$query = "SELECT * FROM 'users'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);

您当前查询的问题是'users'部分.必须使用单引号来分隔字符串,而对于标识符,则必须使用反引号:

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:

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);

在生产服务器上,但是,第一行的值应从1更改为0

on the production server, however, the value on the first line should be changed from 1 to 0

这篇关于如何调试为什么最简单的MySQL查询返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆