1

I am in the process of learning PDO and am trying to implement it in my current project. When I used mysqli, I could get detailed info about any error using mysqli_error($connection). I googled at what the comparable for PDO would be and found this post, and decided to implement it in my code. However, I am unable to get any error messages even when I know there is an obvious error in the sql statement.

Relevant code:

class Database {

public $connection;

function __construct() {
    $this->open_database_connection();
}

public function open_database_connection() {


    try {

        $this->connection = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);

        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->connection->setAttribute( PDO::ATTR_EMULATE_PREPARES, false);


    } catch(PDOException $e) {
        echo $e->getMessage();  
        die('Could not connect');
    }

} // end of open_database_connection method




public function query($sql, $params = []) {

    try {

        $query = $this->connection->prepare($sql);

    } catch (Exception $e) {
        var_dump('mysql error: ' . $e->getMessage());
    }


    foreach($params as $key => $value) {
        if ($key === ':limit') {
            $query->bindValue($key, $value, PDO::PARAM_INT);

        } else {
            $query -> bindValue($key, $value);
        }
    }

    try {

        $query->execute();

    } catch(Exception $e) {
        echo 'Exception -> ';
        var_dump($e->getMessage());
    }


    /*
    DID NOT WORK:
    if (!$query->execute()) {
            print_r($query->errorinfo());

    }*/

      $result = $query->fetchAll(PDO::FETCH_ASSOC);

        $this->confirm_query($result); 
        return $result; 

}  // end of query method



  function confirm_query($query) {

    if (!$query) {
        die('mysql error: ');
}

}

When I run the code, I do get the "mysql error", but I do not get any details about it. What am I doing wrong?

Update: As requested, I am providing additional details below.

What I am trying to do is get the user's login detail to be verified. To that end, once the user inputs his credentials , this code runs:

if (isset($_POST['submit'])) {

    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    //check the database for the user

    $user_found = User::verify_user($username, $password);

Relevant code from the User class:

    public static function verify_user($username, $password) {

        global $db;

        $username = $db->escape_string($username);
        $password = $db->escape_string($password);

        $values = [ ":username" => $username,
                    ":password" => $password,
                    ":limit" => 1
                  ];


        $result_array = self::find_this_query("SELECT * FROM users WHERE username=:username AND password=:password LIMIT :limit", true, $values);


        return !empty($result_array)? array_shift($result_array) : false;

    }




    public static function find_this_query($sql, $prepared = false, $params = []) {

        global $db;

        $the_object_array = array();
        $result = $db->query($sql, $params);

        $arr_length = count($result);

        for ($i = 0; $i < $arr_length; $i++) {

            $the_object_array[] = self::instantiation($result[$i]);
        }

        return $the_object_array;

    }


    public static function instantiation($the_record) {

        $the_object =new self; //we need new self because $the_record corresponds to one user!



        foreach($the_record as $the_attribute => $value) {

            if ($the_object->has_the_attribute($the_attribute)) {
                $the_object->$the_attribute = $value;
            }
        }


        return $the_object;

    }

    public function has_the_attribute($attribute) {

        $object_properties = get_object_vars($this);

        return array_key_exists($attribute, $object_properties);
    }
Community
  • 1
  • 1
Frosty619
  • 1,381
  • 4
  • 23
  • 33

1 Answers1

0

You have to use PDO::errorInfo():

(...)

public function query($sql, $params = []) {

try {

    $query = $this->connection->prepare($sql);
    if( !$query )
    {
       $error = $this->connection->errorInfo();
       die( "mysql error: {$error[2]}" );
    }

} catch (Exception $e) {
    var_dump('mysql error: ' . $e->getMessage());
}

(...)
}

PDO::errorInfo returns an array:

Element 0: SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard);

Element 1: Driver-specific error code;

Element 2: Driver-specific error message.

Community
  • 1
  • 1
fusion3k
  • 11,568
  • 4
  • 25
  • 47