Auswahl einer API

PHP bietet verschiedene APIs für die Verbindung zu MySQL. Im Folgenden werden die APIs beschrieben, die von den Erweiterungen mysqli und PDO bereitgestellt werden. In jedem Codeschnipsel wird eine Verbindung zu einem MySQL-Server auf "example.com" mit dem Benutzernamen "user" und dem Passwort "password" hergestellt und es wird eine Abfrage zur Begrüßung des Benutzers ausgeführt.

Beispiel #1 Vergleich der MySQL-APIs

<?php
// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo
htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo
htmlentities($row['_message']);

Beispiel #2 Vergleich mit vorbereiteten Anweisungen

<?php
// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
$statement = $mysqli->prepare("SELECT District FROM City WHERE Name=?");
$statement->execute(["Amersfoort"]);
$result = $statement->get_result();
$row = $result->fetch_assoc();
echo
htmlentities($row['District']);

// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
$statement = $pdo->prepare("SELECT District FROM City WHERE Name=?");
$statement->execute(["Amersfoort"]);
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo
htmlentities($row['District']);

Vergleich der Merkmale

Die Leistung der beiden Erweiterungen ist insgesamt gesehen in etwa gleich. Allerdings macht die Leistung der Erweiterungen nur einen Bruchteil der Gesamtlaufzeit einer PHP-Webanforderung aus. Oft liegt der Einfluss bei nur 0,1%.

  ext/mysqli PDO_MySQL
PHP-Version der Einführung 5.0 5.1
Enthalten in PHP 7.x und 8.x Ja Ja
Entwicklungsstatus Aktiv Aktiv
Lebenszyklus Aktiv Aktiv
Für neue Projekte empfohlen Ja Ja
OOP-Schnittstelle Ja Ja
Prozedurale Schnittstelle Ja Nein
API unterstützt nicht-blockierende, asynchrone Abfragen mit mysqlnd Ja Nein
Persistente (dauerhafte) Verbindungen Ja Ja
API unterstützt Zeichensätze Ja Ja
API unterstützt serverseitige vorbereitete Anweisungen Ja Ja
API unterstützt clientseitige vorbereitete Anweisungen Nein Ja
API unterstützt gespeicherte Prozeduren Ja Ja
API unterstützt Mehrfachanweisungen Ja Weitestgehend
API unterstützt Transaktionen Ja Ja
Transaktionen können mit SQL gesteuert werden Ja Ja
Unterstützt die gesamte Funktionalität von MySQL 5.1+ Ja Weitestgehend
add a note add a note

User Contributed Notes 4 notes

up
43
alvaro at demogracia dot com
11 years ago
Apart from the feature list, I suggest you try out both MySQLi and PDO and find out what API design you like most. MySQLi is more powerful and probably more complex to learn. PDO is more elegant and has the advantage that you only need to learn one PHP API if you need to work with different DBMS in the future.
up
12
rellect at gmail dot com
5 years ago
Another point to consider and one of the advantages of PDO is that it's not limited to mysql and supports other database engines such as PostgreSQL, SQLite, etc.
up
-44
Anonymous
7 years ago
These are quite possibly the most paradoxical lines in this table:

API supports non-blocking, asynchronous queries with mysqlnd
     ext/mysqli: Yes     PDO_MySQL: No
API supports client-side Prepared Statements
     ext/mysqli: No     PDO_MySQL: Yes

Apparently it's either asynchronous I/O or the security of bound parameters.
up
-59
michaeln at associations plus dot see eh
11 years ago
Another useful consideration to keep in mind when choosing your library is how extensible it is. Chances are, in any sufficiently advanced development scenario, you're going to be extending your database access class to add a method (or multiple methods) for how to handle database errors and alert the development team of errors and whether to have the code fail immediately or fail gracefully serving the user a user-friendly failure notice.

For example, I have a class where I have added extra parameters to the query() function (and a few others), which accept the __FILE__ and __LINE__ constants to facilitate tracking issues. If this were not reasonably possible with PDO-mysql for example (not sure, never used it), it may make one option or the other much less likely to be viable for your usage case.
To Top