Undefined property: PDO::$connect_error

2020-05-06 09:29发布

问题:

I am trying to use $dbc->connect_error to check if any error occurs while trying to connect to my databease. I always get an error page saying:

Notice: Undefined property: PDO::$connect_error in C:\xampp\htdocs\add_products_processing.php on line 7

I am using Windows7 with XAMPP v3.2.2. The full code is shown below. I am sure that the username and the password are correct. Any advice?

<?php

$dsn = 'mysql:host=localhost;dbname=technoglance';
$username = 'root';
$password = 'password';
$dbc = new PDO($dsn, $username, $password);
if ($dbc->connect_error) {
    die("Connection failed: " . $dbc->connect_error);
}

$main_class =filter_input(INPUT_POST, 'main_class');
$brand =filter_input(INPUT_POST, 'brand');
$model =filter_input(INPUT_POST, 'model');
$description =filter_input(INPUT_POST, 'description');
$quantity =filter_input(INPUT_POST, 'quantity');
$adding_date =filter_input(INPUT_POST, 'adding_date');
$sell_price =filter_input(INPUT_POST, 'sell_price');
$buying_price =filter_input(INPUT_POST, 'buying_price');

if(!empty($main_class)){
             try{
            $query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price ) VALUES ('$main_class', '$brand', '$model', '$description', '$quantity', now(),'$sell_price', '$buying_price' );";
             // set the PDO error mode to exception
            $dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbc->exec($query);
             echo "Thank you. The record has been sent successfully.<br><br>";

            }
            catch(PDOException $e){
                echo $query . "<br>" . $e->getMessage()."<br><br>";

            }
}
else{
    echo '<h1>Please use the contact form or don\'t leave an empty field!</h1>';
}

?>

回答1:

Here is what your code should be

<?php

$dsn = 'mysql:host=localhost;dbname=technoglance;charset=utf8';
$username = 'root';
$password = 'password';
$dbc = new PDO($dsn, $username, $password);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// filtering omitted

if(!empty($main_class)){
    $query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price ) VALUES (?,?,?,?,?,?,?,?);";
    $data = [$main_class,$brand,$model,$description,$quantity,$adding_date,$sell_price,$buying_price];
    $dbc->prepare($query)->execute($data);
    echo "Thank you. The record has been sent successfully.<br><br>";}
else{
    echo '<h1>Please use the contact form or don\'t leave an empty field!</h1>';
}

PDO will report it's errors already, without any extra code required.
and you should be using prepared statements



回答2:

If we have a look at the PDO manual and look up for the PDO class there is no connect_error property anywhere. But if we check mysqli manual we see it right there. You have to choose a database library and stick to it, they cannot be mixed.

I always recommend to configure PDO to throw exceptions as you already do (although connection errors in particular will always through an exception no matter your settings) and not care to catch them unless you want to do something specific with them.



回答3:

There is no connect_error. You should use exception:

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Or if you do not want exception you can try errorCode and errorInfo



标签: php pdo