PHP Connect to MySQL Database

In this tutorial, how to PHP connect to PDO and MySQL. PDO means PHP Data Objects and makes connections are established by creating instances of PDO base class.
 
It does not need to matter what driver you are using just use the PDO connection and it takes and accepts the parameters for connecting databases.
 
PDO supported by databases are mysql,postgresql,ms sql server, oracle ,sqlite ...,etc.

The advantage of PDO provides many ways to works with objects and retrieve and work much easier. It's easy to access databases through PHP. 
 
PDO is easy to adapt to different databases and platforms by changing the connection string. 
 
PDO has reduced the line of insert and update database operation into a two-step process, and PDO is full advantages of prepare->bind->execute of the prepared statement which is used to protect against malicious attacks through SQL injection. 
 
They are three pdo classes 
 
PDO - Makes connection between PHP and the database 
PDOstatement- prepared statement and execution 
PDOException- Error appear by pdo. 
 

Enable PDO_MySQL Driver 

 
All of before , we want to enable the driver of mysql in xampp
 
First : open the xampp and right side xampp Action panel choose the config and choose the php.ini (or) manual to open under php ->
C:/xampp/php/php.ini 

second : search the following extension line

PHP Connect to MySQL Database

;extension=php_pdo_mysql.dll 

This extension with semicolon (;) if you want to enable the pdo driver you just need to uncomment the semicolon.

extension=php_pdo_mysql.dll 


PDO Connecting to MySQL


    <?php
$conn = new PDO('mysql:host=localhost;dbname=phptutorial', $user, $password);
?>
    


<?php  
    $host="localhost";  
    $name="phptutorial";  
    $user="root";     
    $password="";     
    try{  
        $conn= new PDO("mysql:host=$host;dbname=$name",$user,$password);  
        echo "Successfully connected";  
    } catch(Exception $e){  
    echo "Connection failed" . $e->getMessage();  
    }  
?>
  
  



<?php
    $host="localhost";  
    $name="phptutorial";  
    $user="root";     
    $password="";   

try {
   $conn= new PDO("mysql:host=$host;dbname=$name",$user,$password);  
  // set the PDO errormode exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Successfully connected";  
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
  

PDO::ATTR_ERRMODE - ERROR REPORTING 
PDO::ERRMODE_EXCEPTION - It's the error code throw exception in PDO. 
 
Invalid Database:

Database name: phptutorial
 
 
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'phptutorials'

Above error I added the 's' last of database name.
 

PHP Connect to MySQL


In the MySQLi - 'i' stands for "improved".
 
In the above code connection string queries are connected with the database using PHP and MySQL and different between PDO and MySQL. 
 
PDO the advantage is already mentioned above and MySQL is need to rewrite the entire code to be included in the connection string.


<?php
$localhost = "localhost";
$username = "root";
$password = "password";

// Create connection
$conn = new mysqli($localhost, $username, $password);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Post a Comment

Previous Post Next Post