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.
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
PDOMySQLi
The Database supports 12 different databases. MySQL is supported.
APIOOPOOP + Process
ConnectionEasyEasy
The specified name parameter is not supported.
Support for object ing
Preprocessing statement
(Client) support not supported
Fast Performance
Support for Stored Procedures
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.