How can I get data from a database using this preparation statement

//this is my connection function. It is connecting databse successfully when I check.
$conn = connection($config['servername'],$config['username'],$config['password']);

after that I used the following code to retrieve data from a database

$id = 2;
if($conn) {

    try {

        $stmt = $conn->prepare('SELECT * FROM customer_tbl WHERE cus_id = :id');
        $stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt->bindParam(':id', $id);

        $results = $stmt->execute();

    }catch (PDOException $e){

        echo 'Error: ' . $e->getMessage();
    }

}

this code shows the following error message in browser

Error: SQLSTATE [IM001]: the driver does not support this function: this driver does not support configuration attributes

what is wrong with my code ?. Why couldn’t I get the data from the database?

if i want to extract this data from databese using make statement how to encode?

+4
source share
1 answer

Add the following

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

after the connection string with $connObject

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

To use data

$stmt->execute();    
$rows= $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rows); // to print an array

. PDO

+7

All Articles