1) The total comparison
|
Pdo |
Mysqli |
Database support |
12 different kinds of database support |
Support MySQL |
Api |
Oop |
OOP + Process |
Connection |
Easy |
Easy |
Named parameters |
Support |
Not supported |
Object Mapping Support |
Support |
Support |
Preprocessing statements Client |
Support |
Not supported |
Performance |
Fast |
Fast |
Supporting stored procedures |
Support |
Support
|
2 connection mode
Let's look at the two ways to connect to a database:
$pdo New PDO ("Mysql:host=localhost;dbname=database", ' username ', ' password ' ) ; // mysqli, process-oriented approach $mysqli Mysqli_connect (' localhost ', ' username ', ' password ', ' database '); // mysqli, Object-oriented $mysqli New
3 Database Support
PDO supports multiple databases, but mysqli only supports MySQL
4 named parameter name parameter
PDO in the way:
$params Array $mail Time ()-3600$pdo->prepare (' SELECT * from users WHERE username =: username and email =: Email and LA St_login >: Last_login
And mysqli is troublesome point, do not support this, can only:
$query $mysqli->prepare ('SELECT * from userswhere username =? and email =? and Last_login >?' ); $query $mail Time ()-3600); $query->execute ();
In this case, a question mark of the order, but also more troublesome, inconvenient.
5 Support for ORM Mappings
For example, there is a class user, as follows:
classuser{ Public $id; Public $first _name; Public $last _name; Public functioninfo () {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 in a process-oriented manner:
if ($resultmysqli_query($mysqli$query)) { while ($user mysqli_fetch_object($result, ' User '){ echo$user Info (). " \ n ";
}}
MYSQLI uses a process-oriented approach:
// mysqli, Object oriented if ($result$mysqli->query ($query)) { while ($user$ Result->fetch_object (' User ') { echo$user->info (). " \ n "; }}
6 Prevention of SQL injection:
PDO Manual Setup
$username = pdo::quote ($_get[' username ']); $pdo $username");
Using mysqli
$username mysqli_real_escape_string ($_get[' username ']); $mysqli->query ("SELECT * from users WHERE username = '$username'");
7 preparestament
PDO mode:
$pdo->prepare (' SELECT * from users WHERE username =: username '); $pdo->execute (array$_get[' username ']));
Mysqli:
$query $mysqli->prepare (' SELECT * from users WHERE username =? ' ); $query $_get [' username ']); $query->execute ();
PHP PDO vs. Mysqli selection