Connect MySQL Database to PHP with PDO & Classes

Without a database, no practical PHP application could not exist. in php, we connect MySQL database by two most common ways: MySQLi(Improved MySQL) and PDO (PHP Data Objects) and along with OOP. in this post we must use PDO way to connecting the MySQL database server to php.

PDO is a database access layer which gives a quick and reliable interface for accessing,managing databases in PHP and more stable extension. PDO will support 12 different database systems, while MySQLi will support only MySQL Database System. PDO & MySQLi will also support an API ‘Object Oriented’.

IF you use another database system (database) something as like ORACLE, SQLite,IBM Db2, Cubrid, Microsoft SQL Server instead of My SQL Database in PHP, PHP Applications then PDO is Best Option of PHP. just change connection string and a few queries.it’s support Prepared statements.it’s also prevents SQL Injection Queries Attacks in PHP.

in PHP 5.1 or Above Version  PDO_MySQL and PDO_SQLITE Driver is pre-default installed and enabled but only one extension default enabled ‘mysql .

if you check what PDO Database System Drivers installed in PHP or your System then create a new PHP file and add the following code into it

Example
Download
 <?php
print_r(PDO::getAvailableDrivers());
?>

Connecting to MySQL Database

Basic Syntax : PHP-PDO DSN Connection String
$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");

PDO accepts the given parameters for specifying the database source (known as DSN) which have contains the database servername or hostname  such as localhost or remote IP address ,database name and other optional parameters such as username and password that helps for connecting to database server.its creates a connection between php,php applications & database server. 

to connect to mysql server, you must use the following data source name or above written syntax code example.the above written syntax code is some different because we use (OOP) classes and function in code. 

$pdo = new PDO("mysql:host=" . $this->db_host .";dbname=" . $this->dbname, $this->db_user , $this->db_pass);
Example
Download
 <?php
$db = new db();
class db
{
   private $db_host = "localhost";
   private $db_user = "root";
   private $db_pass = "";
   private $dbname = "test" ;
   public $error;
   
   protected function connect()
  {
   // $pdo = new PDO("mysql:host=" . $this->db_host .";dbname=" . $this->dbname, $this->db_user , $this->db_pass);
   $dsn = "mysql:host=" . $this->db_host .";dbname=".$this->dbname;
   $pdo = new PDO($dsn, $this->db_user , $this->db_pass);
   $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
   return $pdo; 
  }
    public function __construct() 
 {

       $sql = "CREATE TABLE users ( name VARCHAR(111) NOT NULL , username VARCHAR(111) NOT NULL UNIQUE , password VARCHAR(111) NOT NULL ) ENGINE = InnoDB";
       if($stmt = $this->connect()->query($sql))
       {
          echo "Table created";
       }
       else
       {
         echo "Table already created or Another Problem";
       }
 }
     
}
?>

Check out this also:-

You Can Call Police Without SIM Without Balance

Leave a Reply