7

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.

12
  • 1
    Did u try to run query in phpmyadmin or database you are using ?
    – Milap
    Mar 8, 2012 at 14:58
  • @ashes999 Thanks for an idea. I tried it right now and still I receives return $query = false.
    – tatiana_c
    Mar 8, 2012 at 14:59
  • -0.25 for still using mysql_query.
    – cHao
    Mar 8, 2012 at 14:59
  • @Milap Yes, I tried also now and it works fine in phpadmin.
    – tatiana_c
    Mar 8, 2012 at 15:01
  • @Marcus Adams The "SELECT 1" also returns false. But I tried now also with `` , still $query = false.
    – tatiana_c
    Mar 8, 2012 at 15:04

3 Answers 3

18

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

3
  • It's great. It gives me the errors. I see for now that database is not selected. This is exactly command that I looked for. Thanks!
    – tatiana_c
    Mar 8, 2012 at 15:09
  • mysql_* functions are removed in PHP 7. Do you have an alternative?
    – knt5784
    Feb 27, 2019 at 19:29
  • @knt5784 thank you, I added alternatives for PDO and mysqli Apr 24, 2019 at 13:09
4

Use the mysql_error() function:

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

Here is some more information: http://www.phpfreaks.com/blog/or-die-must-die

9
  • Never use die(). That's extremely bad practice. Mar 8, 2012 at 15:03
  • In a production environment I would completely agree, but this seems to clear be a test environment. Still, thanks for the comment and I'll clarify it in the answer.
    – Ynhockey
    Mar 8, 2012 at 15:05
  • 1
    you are using different codes on the development and production? Really you are? Mar 8, 2012 at 15:07
  • 1
    In case you're wondering why, die(mysql_error()) shows the MySQL error to the end user, which, besides being user unfriendly, is also a potential security risk. Mar 8, 2012 at 15:09
  • Anyway, how it is supposed to track errors on the production? Mar 8, 2012 at 15:09
1

Based on Your Common Sense answer this is an object oriented style:

$query = "SELECT * FROM 'users'";
$result = $mysqli -> query($query) or trigger_error($mysqli -> error." ".$query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.