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.
Make your constructor the proper way
function __construct()
{
$this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
}- 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 untilexecute()
. 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
JSON_Encode Function: Special Characters
Accessing Class Properties with Spaces
Using X-Sendfile with Apache/Php
Get Date Range Between Two Dates Excluding Weekends
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Send Cookie with File_Get_Contents
How to Make Number_Format() Not to Round Numbers Up
The Post Method Is Not Supported for This Route. Supported Methods: Get, Head. Laravel
Strip All HTML Tags, Except Allowed
How to Get Rid of MySQL Error 'Prepared Statement Needs to Be Re-Prepared'
Least Memory Intensive Way to Read a File in PHP
PHP Configuration: It Is Not Safe to Rely on the System's Timezone Settings
How to Replace (Monkeypatch) PHP Functions
How to Retrieve Utf-8 Accented Characters from Access via Pdo_Odbc