Wednesday, 11 July 2018

PHP PDO: Getting Connection Attributes

PHP PDO: Getting Connection Attributes

At several previous tutorial about connection attributes, we talk how to set attributes. Now, we talk about how to get value from connection attributes. We can use getAttribute(). Look this example:
PDO:ATTR_DRIVER_NAME: It returns the name of the underlying database driver.
<?php

// configuration

$dbhost        = "localhost";

$dbname        = "pdo";

$dbuser        = "root";

$dbpass        = "";

// database connection

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));

echo $conn->getAttribute(PDO::ATTR_DRIVER_NAME);

// result: mysql

?>

Else example:

<?php

// configuration

$dbhost        = "localhost";

$dbname        = "pdo";

$dbuser        = "root";

$dbpass        = "";

// database connection

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));

echo $conn->getAttribute(PDO::ATTR_DRIVER_NAME);

echo "<br>";

echo $conn->getAttribute(PDO::ATTR_CLIENT_VERSION);

echo "<br>";

echo $conn->getAttribute(PDO::ATTR_SERVER_VERSION);

?>

0 comments:

Post a Comment