1) Overall comparison
|
PDO |
MySQLi |
数据库支持 |
12种不同的数据库支持 |
支持MySQL |
API |
OOP |
OOP + 过程 |
Connection |
Easy |
Easy |
命名参数 |
支持 |
不支持 |
对象映射支持 |
支持 |
支持 |
预处理语句
(客户端) |
支持 |
不支持 |
性能 |
快 |
快 |
支持存储过程 |
支持 |
支持 |
2 Connection methods
Let’s first 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 approach
$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 named parameter 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');
But MYSQLI is a bit more troublesome. It does not support this, so it can only:
The code is as follows:
$query = $mysqli->prepare('
SELECT * FROM users
WHERE username = ?
AND email = ?
AND last_login > ?');
$query->bind_param('sss', 'test', $mail, time() - 3600);
$query->execute();
In this case, it would be troublesome and inconvenient to order the question marks one by one.
5 ORM mapping support
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 Prevent SQL injection:
PDO Manual Settings
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 method:
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();
Have you gained a new understanding of PHP’s two linking methods, PDO and mysqli, through this article? I hope this article can be helpful to you.
http://www.bkjia.com/PHPjc/960577.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/960577.htmlTechArticleComparative analysis of the database connection methods pdo and mysqli in php. This article mainly introduces the database connection methods pdo and mysqli in php. A comparative analysis has been conducted from various aspects, which is very comprehensive. It is recommended here...