mysql 查询false_如何调试为什么最简单的MySQL查询返回false?

bd96500e110b49cbb3cd949968f18be7.png

I work with xampp. I performed MySQL connection:

$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.

But the simplest query like:

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

returns false. How can I debug why?Thanks.

解决方案

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:

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

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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

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

As of the old 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:

$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:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值