Database connection methods in PHP _php tutorial for PDO and mysqli comparison analysis

Source: Internet
Author: User

Database connection in PHP comparison of PDO and mysqli


This article mainly introduces the database connection method in PHP PDO and mysqli from all aspects of the comparative analysis, very comprehensive, here is recommended to everyone, there is a need for small partners to reference.

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:

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 in the way:

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 ');

And mysqli is troublesome point, do not support this, 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, 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:

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 in a process-oriented manner:

The code is as follows:


if ($result = Mysqli_query ($mysqli, $query)) {
while ($user = Mysqli_fetch_object ($result, ' user ')} {
echo $user->info (). " \ n ";
}
}

MYSQLI uses a process-oriented approach:

The code is as follows:


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

The code is as follows:


$username = pdo::quote ($_get[' username ');
$pdo->query ("SELECT * from users WHERE username = $username");

Using 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 ();

Whether the small partners through this article for PHP 2 ways to link PDO and mysqli have a new understanding of it, I hope this article can help you.

http://www.bkjia.com/PHPjc/960577.html www.bkjia.com true http://www.bkjia.com/PHPjc/960577.html techarticle database Connection in PHP comparison analysis of PDO and mysqli This article mainly introduces the database connection method in PHP PDO and mysqli from all aspects of the comparative analysis, very comprehensive, recommended here ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.