Comparison and analysis of database connection methods pdo and mysqli in php _ PHP Tutorial

Source: Internet
Author: User
Tags sql injection prevention
Comparison and analysis of the database connection methods pdo and mysqli in php. Comparison and analysis of database connection methods pdo and mysqli in php this article mainly introduces the comparison and analysis of database connection methods pdo and mysqli in php, which is very comprehensive, we recommend that you compare and analyze the database connection methods pdo and mysqli in php.

This article mainly introduces the database connection methods in php, pdo and mysqli, which have been compared and analyzed in various aspects. it is very comprehensive. we recommend this article to you and you can refer to it as needed.

1) overall comparison

PDO MySQLi
Database support Support for 12 different databases Support for MySQL
API OOP OOP + process
Connection Easy Easy
Naming parameters Supported Not supported
Object ing support Supported Supported
Preprocessing statement
(Client)
Supported Not supported
Performance Fast Fast
Support stored procedures Supported Supported

2 Connection mode

First, let's take a look at how the two connect to the database:

The code is as follows:


// PDO
$ Pdo = new PDO ("mysql: host = localhost; dbname = database", 'username', 'password ');
// Mysqli, process-oriented
$ Mysqli = mysqli_connect ('localhost', 'username', 'password', 'database ');
// Mysqli, object-oriented
$ Mysqli = new mysqli ('localhost', 'username', 'password', 'database ');

3. database support

PDO supports multiple databases, but MYSQLI only supports MYSQL

4. name parameter

PDO method:

The code is as follows:


$ Params = array (': username' => 'test',': email '=> $ mail,': last_login '=> time ()-3600 );
$ Pdo-> prepare ('
SELECT * FROM users
WHERE username =: username
AND email =: email
AND last_login>: last_login ');

However, MYSQLI is troublesome and does not support this. it can only be:

The code is as follows:


$ Query = $ mysqli-> prepare ('
SELECT * FROM users
WHERE username =?
AND email =?
AND last_login>? ');
$ Query-> bind_param ('SS', 'test', $ mail, time ()-3600 );
$ Query-> execute ();

In this case, the order of question marks is troublesome and inconvenient.

5. support for ORM ing

For example, there is a class user, as follows:

The code is as follows:


Class User
{
Public $ id;
Public $ first_name;
Public $ last_name;
Public function info ()
{
Return '#'. $ this-> id. ':'. $ this-> first_name. '. $ this-> last_name;
}
}
$ Query = "SELECT id, first_name, last_name FROM users ";
// PDO
$ Result = $ pdo-> query ($ query );
$ Result-> setFetchMode (PDO: FETCH_CLASS, 'User ');
While ($ user = $ result-> fetch ())
{
Echo $ user-> info (). "\ n ";
}

MYSQLI uses a process-oriented approach:

The code is as follows:


If ($ result = mysqli_query ($ mysqli, $ query )){
While ($ user = mysqli_fetch_object ($ result, 'User ')){
Echo $ user-> info (). "\ n ";
}
}

MYSQLI adopts a process-oriented approach:

The code is as follows:


// MySQLi, object oriented way
If ($ result = $ mysqli-> query ($ query )){
While ($ user = $ result-> fetch_object ('user ')){
Echo $ user-> info (). "\ n ";
}
}

6. SQL Injection Prevention:

Manually set PDO

The code is as follows:


$ Username = PDO: quote ($ _ GET ['username']);
$ Pdo-> query ("SELECT * FROM users WHERE username = $ username ");

Use mysqli

The code is as follows:


$ Username = mysqli_real_escape_string ($ _ GET ['username']);
$ Mysqli-> query ("SELECT * FROM users WHERE username = '$ username '");

7 preparestament

PDO mode:

The code is as follows:


$ Pdo-> prepare ('select * FROM users WHERE username =: username ');
$ Pdo-> execute (array (': username' = >$ _ GET ['username']);

MYSQLI:

The code is as follows:


$ Query = $ mysqli-> prepare ('select * FROM users WHERE username =? ');
$ Query-> bind_param ('s ', $ _ GET ['username']);
$ Query-> execute ();

Are you familiar with the two link methods PDO and mysqli in PHP? I hope this article will help you.

This article mainly introduces the database connection methods in php, pdo and mysqli, which are compared and analyzed in various aspects and are very comprehensive. we recommend this article...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.