PHP PDO vs. Mysqli selection

Source: Internet
Author: User

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:

$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

3 Database Support
PDO supports multiple databases, but mysqli only supports MySQL
4 named parameter name parameter

PDO in the way:

$params Array $mail  Time ()-3600$pdo->prepare (' SELECT * from users WHERE username =: username and email =: Email and LA St_login >: Last_login

And mysqli is troublesome point, do not support this, can only:

$query $mysqli->prepare ('SELECT * from userswhere username =? and email =? and Last_login >?' ); $query $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:

classuser{ Public $id;  Public $first _name;  Public $last _name;  Public functioninfo () {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:

if ($resultmysqli_query($mysqli$query)) {   while ($user  mysqli_fetch_object($result, ' User '){  echo$user Info (). " \ n ";
}}

MYSQLI uses a process-oriented approach:

// 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

$username = pdo::quote ($_get[' username ']); $pdo $username");

Using mysqli

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

7 preparestament
PDO mode:

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

Mysqli:

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

PHP PDO vs. Mysqli selection

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.