PHP mysqli select_db()関数
例-オブジェクト指向スタイル
接続のデフォルトデータベースを変更します。
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Return name of current default database
if ($result = $mysqli
-> query("SELECT DATABASE()")) {
$row = $result -> fetch_row();
echo "Default database is " . $row[0];
$result -> close();
}
// Change db
to "test" db
$mysqli -> select_db("test");
// Return name of current
default database
if ($result = $mysqli -> query("SELECT DATABASE()")) {
$row = $result -> fetch_row();
echo "Default database is " .
$row[0];
$result -> close();
}
$mysqli -> close();
?>
下部にある手続き型の例を見てください。
定義と使用法
select_db()/ mysqli_select_db()関数は、接続のデフォルトデータベースを変更するために使用されます。
構文
オブジェクト指向スタイル:
$mysqli ->
select_db(name)
手続き型:
mysqli_select_db(connection, name)
パラメータ値
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
name | Required. Specifies the database name |
技術的な詳細
戻り値: | 成功するとTRUE。失敗するとFALSE |
---|---|
PHPバージョン: | 5歳以上 |
例-手続き型
接続のデフォルトデータベースを変更します。
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// Return name of current default database
if ($result = mysqli_query($con, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
echo "Default database is " . $row[0];
mysqli_free_result($result);
}
// Change db
to "test" db
mysqli_select_db($con, "test");
// Return name of current default database
if ($result = mysqli_query($con, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
echo "Default database is " . $row[0];
mysqli_free_result($result);
}
// Close connection
mysqli_close($con);
?>
❮PHPMySQLiリファレンス