Why we should use PHP’s PDO instead of mysql extension

No comments
As we all know that, mysql extension is deprecated and will be removed in the future. So now we must have to migrate from mysql to mysqli or PDO. PDO extension is available in PHP 5.1 or later version. Here we learn step by step about PDO.

What is PDO?

PDO stand for PHP data objects. Php.net defines PDO, ‘PDO is lightweight, consistent interface for accessing databases in PHP’.  PDO provides same function for execute queries for all the databases which supports its extension. It means, you don’t need to change codes.

What is defference between Mysqli and PDO?

PDO and Mysqli both are same except two major differences.
  1.  Mysqli support only MySQL database whereas PDO Support 12 different databases.
  2.  Unlike Mysqli, PDO does not have procedural fallback. For example, mysql_*functions

Getting started

In order to use PDO, first check that your server has installed PDO extension. You can check this by running $check_pdo = new PDO(); and see what it says.

Database connection:

try {
    $db = new PDO('mysql:dbname=mydb;host=localhost', 'username', 'password');
} catch(PDOException $e) {
    die('Could not connect to the database:<br/>' . $e);

Here mysql in parameter is extension name. You can use mssql or other supported database extensions.

Select query

$statement = "SELECT * FROM USERS";
$users = $db->query($statement);

No comments :

Post a Comment