Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

php - How can I debug why simplest MySQL query returns false?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...