Database connection mode in PHP PDO and mysqli contrast analysis _php skills

Source: Internet
Author: User
Tags prepare sql injection

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 does not support
Object Mapping Support Support Support
Preprocessing statements
Client
Support does not support
Performance Fast Fast
Support Stored Procedures Support Support

2 connection mode

Let's look at how the two connect to the database:

Copy Code code 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 the way:

Copy Code code 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 trouble point, do not support this, can only:

Copy Code code 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, the order of the question mark, but also more trouble, not convenient.

5 Support for ORM Mappings

For example, there is a class user, as follows:

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

Copy Code code as follows:

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

Mysqli Adopt a process-oriented approach:

Copy Code code as follows:

Mysqli, Object Oriented way
if ($result = $mysqli->query ($query)) {
while ($user = $result->fetch_object (' user ')) {
echo $user->info (). \ n ";
}
}

6 Prevention of SQL injection:

PDO Manual Settings

Copy Code code as follows:

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

Using mysqli

Copy Code code as follows:

$username = mysqli_real_escape_string ($_get[' username '));
$mysqli->query ("SELECT * from users WHERE username = ' $username '");

7 preparestament

PDO Way:

Copy Code code as follows:

$pdo->prepare (' SELECT * from users WHERE username =: username ');
$pdo->execute (': username ' => $_get[' username '));

Mysqli:

Copy Code code as follows:

$query = $mysqli->prepare (' SELECT * from users WHERE username =? ');
$query->bind_param (' s ', $_get[' username ']);
$query->execute ();

Small partners whether through this article for PHP 2 kinds of link way PDO and mysqli have a new understanding of it, I hope this article can help.

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.