Comparative analysis of database connection methods pdo and mysqli in php_PHP tutorial

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

Comparative analysis of the database connection methods pdo and mysqli in php

This article mainly introduces the comparative analysis of the database connection methods pdo and mysqli in php from all aspects, which is very comprehensive. It is recommended here to everyone, and friends who need it can refer to it.

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.

www.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...
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