Home > Backend Development > PHP Tutorial > Comparison of usage of php pdo and mysqli to connect to mysql

Comparison of usage of php pdo and mysqli to connect to mysql

WBOY
Release: 2016-07-25 08:51:42
Original
859 people have browsed it
  1. // pdo

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

  3. $mysqli = mysqli_connect('localhost','username','password','database');

  4. // mysqli, object-oriented

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

Copy code

3. Database support pdo supports multiple databases, but mysqli only supports mysql

4. Name parameter name parameter pdo method:

  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

MySQL is more troublesome and does not support this. 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 the code

In this case, it will be more troublesome to sequence the question marks one by one. Not very convenient.

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 uses a process-oriented approach: " n";

  }
}
  1. Copy code
  2. 6. Prevent sql injection (analysis of PHP methods to prevent sql injection): pdo manual setting
$username = pdo::quote($_get['username']);
$pdo->query("select * from users where username = $username");

Copy code
  1. Use mysqli:
$username = mysqli_real_escape_string($_get['username']);
$mysqli->query("select * from users where username = '$username'");

Copy code
  1. 7.preparestament pdo mode:
$pdo->prepare('select * from users where username = :username');
$pdo->execute(array(':username' => $_get['username'] ));

Copy code
  1. mysqli way:
$query = $mysqli->prepare('select * from users where username = ?');
$query->bind_param('s', $_get['username']);

$query->execute();

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