PDO Vs MYSQLI

  • 21-Sep, 2018
  • 278 Views
  • 0 Comments

PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code. PDO also has different drivers for different SQL database vendors which will allow you to easily use other vendors without having to relearn a different interface. With the help of PDO developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API. PDO also has multiple methods of error handling. The biggest issue I have seen with mysqli code is that it lacks consistent handling, or no handling at all. With PDO in exception mode, you can get consistent error handling which will end up saving you loads of time tracking down issues.

The MySQLi is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases. MySQLi is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQLi Extension is developed, marketed, and supported by MySQL.

Difference Between PDO and Mysqli

1.  Both PDO and MySQLi provides an object-oriented approach, but MySQLi provides a procedural way also like old mysql extension. Mysqli is only for the MySQL database. PDO supports a variety of other brands of database using the same functions. Mysqli supports prepared statements with "?" placeholders for parameters. PDO supports both "?" placeholders and also named placeholders, like ":columnName".

2.  The main advantage of PDO over MySQLi is its database driver support. PDO supports 12 different drivers and MySQLi supports MySQL only. But new users may prefer MySQLi because of procedural interface which is similar to old mysql extension, So the migrating from the old mysql extension is easier. But after mastering in PDO, you can use it with any database you like. Mysqli requires that you use a function to bind each parameter value to the prepared statement. PDO also allows you to simply pass an array of parameter values as you execute the prepared statement.

3.  When you have to switch your project to use another database, PDO makes the process transparent. So all you will have to do is change the connection string and a few queries. if they use any methods which aren't supported by your new database. With MySQLi, you will need to rewrite every code queries included. Both libraries provide SQL injection security.

4.  Both PDO and MySQLi provides an object-oriented API, but MySQLi also provides a procedural API - which makes it easier for new users to understand. If you are familiar with the native PHP MySQL driver, you will find migration to the procedural MySQLi interface much easier. On the other hand, once you master PDO, you can use it with any database you desire.

5.  In order to store or access the data inside a MySQL database, you first need to connect to the MySQL database server. PHP offers two different ways to connect to MySQL server: MySQLi (Improved MySQL) and PDO (PHP Data Objects) extensions.

 

Syntax of PDO

            

         $servername = "localhost";
         $username = "username";
         $password = "password";

    try {
        //Creating connection for mysql
         $conn = new PDO("mysql:host=$servername;dbname=databasenames", $username, $password);
        // set the PDO error mode to exception
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         echo "Connected successfully";
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
     ?>

 

Syntax of Mysqli

     

        $servername = "localhost";
        $database = "dbname";
        $username = "username";
        $password = "password";

    // Create connection

        $conn = mysqli_connect($servername, $username, $password, $database);

    // Check connection

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());

    }
        echo "Connected successfully";
         mysqli_close($conn);
    ?>

 

 

 

 

Leave a Comment

Name

E - mail

Website

Message

Similar Blogs

Digital Marketing

19-Sep, 2018

234 Views

Benefit Of Dynamic Website

10-Sep, 2018

194 Views

Importance Of Website For Business

06-Sep, 2018

222 Views

up