0

So I'm trying to connect to a DB with PDO. I was able to do this earlier today, but now I'm not sure if its working because when I enter wrong credentials I get errors. Here's my code

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = "localhost";
$pass = "root";
$dbname = "users";
$user = "root";

try{
    $con->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    $con = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

 }
 catch(PDOException $e){
    echo $->getMessage();
 }

When I run any other script they work fine. Anyone see any errors? Also this is what I see in my dev tools when running it. This only happens with the code above.

enter image description here

2 Answers 2

3

Your default setting of display_errors is 0. And you have fatal error. So your code have not been executed and display_errors was not changed, hence you have not recieved error message. You could change your ini settings to display errors.

Or even better - use ide with error highlighting.

You should rewrite your code.

try{
    $con = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); // change order
    $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // change to exception, remove pdo->
 }
 catch(PDOException $e){
    echo $e->getMessage(); // <- fixed
 }
Sign up to request clarification or add additional context in comments.

Comments

0

If you want it throw exceptions, you need to enable to tell it do that.

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

Furthermore, try setting error_reporting(-1) to enable them all.

catch(PDOException $e){
    echo $e->getMessage();
}

1 Comment

Just fixed it. And still the same issue.

Your Answer

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