How to Debug Why Simplest MySQL Query Returns False

How can I debug why simplest MySQL query returns false?

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

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

PHP: MYSQL Query returns wrong value

First check what is actually the query and returned result:

echo $query; // debug query
var_dump($row); // see what is returned

I would suggest you replace the sql query (unless you really have to have it executed like that) with simpler query, and work out the logic (true, false) with php:

$query = "SELECT 1 FROM fachtyp WHERE `Description`='$fachtyp'";
$result = mysqli_query($con, $query);
$exists = (bool) mysqli_num_rows($result); // just count returned rows is enough to determine if value exists in database
if (!$exists) { /* update */ }

Also if $fachtyp value is coming from request (GET or POST value, actually even if it does not come from request), you should use mysqli_real_escape_string() to sanitize value before putting it into query.

How to check if query returned true or false in replacement for mysql_error() in PDO

We set PDO in exception mode.

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

We wrap queries with try/catch block. If an Exception is thrown, we catch it. That's the equivalent of using if(!mysql_query($query)) echo mysql_error();

Your example would be

try
{
$query = $db->prepare("SELECT * FROM login WHERE username=:username AND password=:password");
$query->bindParam(':username',$username);
$query->bindParam(':password',$password);
$query->execute();

$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo "Whoopsie, an error occurred! Message: ". $e->getMessage();
}

mysql query occasionally returns nothing

searching for php "supplied argument is not a valid mysql result resource" reveals that to get the actual error, you'd need to call mysql_error, and the error that you get is because the result of the query is FALSE - this value not being a valid mysql result resource.

i.e. in short you have something like:

$res = FALSE; # should contain the mysql result but does not, due to error.
$cfg = htmlspecialchars(mysql_result($res,0)); # the attempt to call mysql_result on invalid argument errors out.

So you'd want to use something like this:

$query = "SELECT * FROM cats WHERE id=$id";
$qr1 = mysql_query ($query)
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

You might want to give this a shot and see what the underlying error message says.


Given that the error is "MySQL server has gone away", There can be multitude of reasons for it - this article would be a good start to investigate. Searching suggests also some php-related and stack-specific bugs, so it looks like you might need to debug it with a closer attention.

Maybe try to duplicate the setup on another box and then start experimenting with the versions/settings, and see if any of the already reported scenarios match your case. Unfortunately, seems there's no single simple answer to this.

Executing SELECT query return false

It doesn't work, because you're using PDO for connection and mysqli to query. Change your connection to mysqli

$con = new mysqli($servername, $username, $password, $dbname);

or change your query to PDO

$query  = $con->query("SELECT * FROM songs");

CakePHP returning False on successful query

Majority of the time, CakePHP queries are validation errors, try:

if ( $query ) 
debug($this->validationErrors); die();

Not sure what type of query you are trying to run, maybe post it here?
You should try to avoid $this->query in CakePHP, below are some techniques to save/retrieve data in the model.

To save data to the database in CakePHP, use:

$this->save($data);

To retrieve data in the database, use:

$this->find('all'); or $this->find('first');


Related Topics



Leave a reply



Submit