-
-
// pdo
- $pdo = new pdo("mysql:host=localhost;dbname=database", 'username', 'password');
- $mysqli = mysqli_connect('localhost','username','password','database');
// mysqli, object-oriented
- $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:
-
- $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');
Copy code
MySQL is more troublesome and does not support this. Only:
-
- $query = $mysqli->prepare('
- select * from users
- where username = ?
- and email = ?
- and last_login > ?');
- $query->bind_param(' sss', 'test', $mail, time() - 3600);
- $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:
-
- 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";
- }
Copy code
mysqli uses a process-oriented approach:
" n"; }}
- Copy code
-
-
- 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
$username = mysqli_real_escape_string($_get['username']); $mysqli->query("select * from users where username = '$username'");
Copy code - 7.preparestament
pdo mode:
-
-
$pdo->prepare('select * from users where username = :username'); $pdo->execute(array(':username' => $_get['username'] ));
Copy code
$query = $mysqli->prepare('select * from users where username = ?'); $query->bind_param('s', $_get['username']); $query->execute();
|