Pdo Error Message

PDO error message?

Try this instead:

print_r($sth->errorInfo());

Add this before your prepare:

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

This will change the PDO error reporting type and cause it to emit a warning whenever there is a PDO error. It should help you track it down, although your errorInfo should have bet set.

PDO errorInfo Full List and specifically error code 1451

A List of MySQL Errors with its Messages can be found here: Error List

Following this list your error code is only for this specific message.

PHP PDO Hide Error message when failed connecting to MySQL server

} catch (PDO $e) {

PDO is not even exception.

You should use:

} catch (PDOException $e) {

See connection example with error handling

How to turn off PDO error message

PDO::__construct will always throw a PDOException if the connection fails. There is nothing you can do about that. Any sane application should throw exception if it fails to connect or maybe you have good reason to explain why you need to turn off exception on connections.

Beside the connection It will work on others transactions.

Example:

<?php
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_PERSISTENT => false,
);
$dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['database'] . ';charset=utf8';
try {
//THIS WILL THROW EXECPTION IF FAILED, NO MATTER WHAT ERROR MODE YOU SET
$this->dbh = new PDO($dsn, $config['username'], $config['password'], $options);
} catch (PDOException $e) {
// Do something
}

//JUST SILENT THIS WILL NOT CAUSE EXCEPTION
$dbh->query("SELECT badColumn FROM wrongTable");
?>

PDO error message: Call to a member function errorInfo() on a non-object in

You have this message because your error handling is wrong.

  1. Make your constructor the proper way

    function __construct()
    {
    $this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
    }
  2. Read the error message and fix the error

PDO error code always 00000 even when there is an error

I tested this code with PHP 7.1.23:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false) {
echo "error on prepare()\n";
print_r($pdo->errorInfo());
}
if ($sth->execute() === false) {
echo "error on execute()\n";
print_r($sth->errorInfo());
}

Output:

error on execute()
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)

Then I tested the same code, except after disabling emulated prepare:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Output:

error on prepare()
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean

Moral of the story:

  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).

  • When checking for an error on prepare(), use $pdo->errorInfo().

  • When checking for an error on execute(), use $stmt->errorInfo().



Related Topics



Leave a reply



Submit