downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

どのライブラリを選ぶか> <用語説明
[edit] Last updated: Fri, 17 May 2013

view this page in

どの API を使うか

PHP には、MySQL への接続用の API が三種類あります。 以下に、mysql と mysqli そして PDO が提供する API を示します。 それぞれのコードは、"example.com" 上で稼働する MySQL サーバーに ユーザー名 "user"、パスワード "password" で接続するものです。 そして、クエリを実行してユーザーにあいさつします。

例1 三種類の MySQL 用 API の比較

<?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']);

// mysql
$c mysql_connect("example.com""user""password");
mysql_select_db("database");
$result mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row mysql_fetch_assoc($result);
echo 
htmlentities($row['_message']);
?>

おすすめの API

mysqliPDO_MySQL のどちらかを使うことをおすすめします。 古い mysql 拡張モジュールを新規開発で使うのはおすすめしません。 というのも、これは PHP 5.5.0 で非推奨となり、将来的に削除される予定だからです。 詳しい機能比較を以下に示します。全体的なパフォーマンスは、どれもほぼ同じです。 拡張モジュール自体のパフォーマンスが PHP のウェブリクエストの実行時間に及ぼす影響はごくわずかで、 たいていは 0.1% 程度に過ぎません。

機能比較

  ext/mysqli PDO_MySQL ext/mysql
どのバージョンの PHP から使えるか 5.0 5.1 2.0
PHP 5.x に同梱されているか Yes Yes Yes
開発状況 進行中 進行中 保守対応のみ
ライフサイクル 活動中 活動中 非推奨
新規開発でおすすめできるか Yes Yes No
オブジェクト指向のインターフェイス Yes Yes No
手続き型のインターフェイス Yes No Yes
mysqlnd によるノンブロッキングな非同期クエリ Yes No No
持続的接続 Yes Yes Yes
文字セット Yes Yes Yes
サーバーサイドのプリペアドステートメント Yes Yes No
クライアントサイドのプリペアドステートメント No Yes No
ストアドプロシージャ Yes Yes No
複数ステートメント Yes Most No
トランザクション Yes Yes No
SQL によるトランザクション制御 Yes Yes Yes
MySQL 5.1+ の全機能への対応 Yes Most No


どのライブラリを選ぶか> <用語説明
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes どの API を使うか - [3 notes]
up
2
michaeln at associations plus dot see eh
2 months 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.
up
5
alvaro at demogracia dot com
11 months 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
0
roland at mxchange dot org
1 month ago
To solve PDO limitations as described by michaeln at associations plus dot see eh, you may try to overwrite PDO methods:

<?php
class MyPdo extends PDO {
// What ever parameters PDO wants + prepend some own:
public function query($caller, $line, $foo, $bar, $bazz) {
// Do your stuff here with $caller, $line (e.g. logging for debug purposes)
// Then call the extended method
return parent::QUERY($foo, $bar, $bazz) or $this->myErrorHandler($caller, $line, $someSqlToLog);
}
}
?>

This is only a very basic example. You may otherwise choose to "wrap" the PDO class and save an instance of the "underlaying" PDO class inside the wrapper class. This is maybe a little more complex approach but allows you to have on interfaces in class signature + way better flexibility.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites