Comparison and analysis of the database connection methods pdo and mysqli in php, pdomysqli. Comparison and analysis of database Connection methods pdo and mysqli in php, pdomysqli1) comparison of PDOMySQLi databases support 12 different databases support MySQL apioopoop + comparison and analysis of database Connection methods pdo and mysqli in Connection php, pdomysqli
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.
Comparison 1) In general, the PDO MySQLi database supports 12 different databases and supports MySQL api oop + process Connection...