PHP PDO vs. mysqli connection MySQL usage comparison

Source: Internet
Author: User
    1. Pdo

    2. $pdo = new PDO ("Mysql:host=localhost;dbname=database", ' username ', ' password ');

    3. MYSQLI, process-oriented approach

    4. $mysqli = Mysqli_connect (' localhost ', ' username ', ' password ', ' database ');

    5. Mysqli, Object-oriented

    6. $mysqli = new mysqli (' localhost ', ' username ', ' password ', ' database ');

Copy Code

3, the database support PDO support a variety of databases, but mysqli only support MySQL

4. Named parameter name Parameterpdo mode:

    1. $params = Array (': username ' = ' = ' Test ', ': email ' + $mail, ': last_login ' = time ()-3600);
    2. $pdo->prepare ('
    3. SELECT * from Users
    4. WHERE Username =: username
    5. and email =: Email
    6. and Last_login >: Last_login ');
Copy Code

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

    1. $query = $mysqli->prepare ('
    2. SELECT * from Users
    3. where username =?
    4. and email =?
    5. and Last_login >? ');
    6. $query->bind_param (' sss ', ' Test ', $mail, Time ()-3600);
    7. $query->execute ();
Copy Code

In this case, a question mark of the order, but also more troublesome, inconvenient.

5, ORM Mapping support for example, there is a class user, for example:

    1. Class User
    2. {
    3. public $id;
    4. Public $first _name;
    5. Public $last _name;
    6. Public Function info ()
    7. {
    8. Return ' # '. $this->id. ': ' . $this->first_name. ' ' . $this->last_name;
    9. }
    10. }
    11. $query = "SELECT ID, first_name, last_name from users";
    12. Pdo
    13. $result = $pdo->query ($query);
    14. $result->setfetchmode (pdo::fetch_class, ' User ');
    15. while ($user = $result->fetch ())
    16. {
    17. echo $user->info (). "\ n";
    18. }
Copy Code

Mysqli in a process-oriented manner:

    1. if ($result = Mysqli_query ($mysqli, $query)) {
    2. while ($user = Mysqli_fetch_object ($result, ' user ')} {
    3. echo $user->info (). " \ n ";
    4. }
    5. }
Copy Code

6, prevent SQL injection (PHP to prevent SQL injection method parsing): PDO manual Setup

    1. $username = pdo::quote ($_get[' username ');
    2. $pdo->query ("SELECT * from users where username = $username");
Copy Code

Using mysqli:

    1. $username = mysqli_real_escape_string ($_get[' username ');
    2. $mysqli->query ("SELECT * from users where username = ' $username '");
Copy Code

7, Preparestamentpdo Way:

    1. $pdo->prepare (' select * from users where Username =: username ');
    2. $pdo->execute (Array (': username ' = $_get[' username '));
Copy Code

Mysqli Way:

    1. $query = $mysqli->prepare (' select * from users where username =? ');
    2. $query->bind_param (' s ', $_get[' username ');
    3. $query->execute ();
Copy Code
  • 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.