In some situations its useful to use this function for changing databases in general. We've tested it in production environment and it seams to be faster with switching databases than creating new connections.
mysqli::select_db
mysqli_select_db
(PHP 5)
mysqli::select_db -- mysqli_select_db — クエリを実行するためのデフォルトのデータベースを選択する
説明
オブジェクト指向型
bool mysqli::select_db
( string
$dbname
)手続き型
データベース接続に対してクエリを実行する際に使用する、 デフォルトのデータベースを設定します。
注意:
この関数は、接続のデフォルトデータベースを変更する際にのみ使用します。 デフォルトデータベースは、mysqli_connect() の 4 番目の引数でも指定可能です。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例1 mysqli::select_db() の例
オブジェクト指向型
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* 現在のデフォルトデータベース名を返します */
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
/* データベースを world に変更します */
$mysqli->select_db("world");
/* 現在のデフォルトデータベース名を返します */
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
$mysqli->close();
?>
手続き型
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* 現在のデフォルトデータベース名を返します */
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
printf("Default database is %s.\n", $row[0]);
mysqli_free_result($result);
}
/* データベースを world に変更します */
mysqli_select_db($link, "world");
/* 現在のデフォルトデータベース名を返します */
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
printf("Default database is %s.\n", $row[0]);
mysqli_free_result($result);
}
mysqli_close($link);
?>
上の例の出力は以下となります。
Default database is test. Default database is world.
pjasiulewicz at gmail dot com
27-Mar-2011 02:30
