Last Updated: March 29, 2017
· oliverusselldev

PDO Database Connection

By default, PHP has PDO_SQLite driver installed. However, if you wish to work with other databases, you must first install the relevant driver.

In order to check what drivers are installed on your system, create a new PHP file and add the following code snippet to it:




Creating a Table With PDO


include_once 'connection.php';



     $database = new Connection();

     $db = $database->openConnection();

     // sql to create table

     $sql = "CREATE TABLE `Student` ( `ID` INT NOT NULL AUTO_INCREMENT , `name`VARCHAR(40) NOT NULL , `last_ame` VARCHAR(40) NOT NULL , `email` VARCHAR(40)NOT NULL , PRIMARY KEY (`ID`)) ";

     // use exec() because no results are returned


     echo "Table Student created successfully";



catch (PDOException $e)


    echo "There is some problem in connection: " . $e->getMessage();