Comparative analysis of database connection methods pdo and mysqli in php, pdomysqli_PHP tutorial

WBOY
Release: 2016-07-13 10:06:07
Original
736 people have browsed it

Comparative analysis of pdo and mysqli database connection methods in php, pdomysqli

1) Overall comparison

  PDO MySQLi
数据库支持 12种不同的数据库支持 支持MySQL
API OOP OOP + 过程
Connection Easy Easy
命名参数 支持 不支持
对象映射支持 支持 支持
预处理语句 
(客户端)
支持 不支持
性能
支持存储过程 支持 支持
PDO
MySQLi
Database support 12 different databases supported Support MySQL
API OOP OOP + Process
Connection Easy Easy
Named parameters Support Not supported
Object mapping support Support Support
Prepared statements
(Client)
Support Not supported
Performance Fast Fast
Support stored procedures Support Support

2 Connection methods

Let’s first look at how the two connect to the database:

Copy code 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:

Copy code 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:

Copy code 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:

Copy code 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:

Copy code 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:

Copy code 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 Preventing SQL injection:

PDO Manual Settings

Copy code The code is as follows:

$username = PDO::quote($_GET['username']);
$pdo->query("SELECT * FROM users WHERE username = $username");

Use mysqli

Copy code The code is as follows:

$username = mysqli_real_escape_string($_GET['username']);
$mysqli->query("SELECT * FROM users WHERE username = '$username'");

7 preparestament

PDO method:

Copy code The code is as follows:

$pdo->prepare('SELECT * FROM users WHERE username = :username');
$pdo->execute(array(':username' => $_GET['username']));

MYSQLI:

Copy code 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/960716.htmlTechArticleComparative analysis of pdo and mysqli database connection methods in php, pdomysqli 1) General comparison PDO MySQLi database supports 12 different Database support supports MySQL API OOP OOP + process Connection...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!