mysqli_select_db

(PHP 5)

mysqli_select_db

(no version information, might be only in CVS)

mysqli->select_db -- クエリを実行するためのデフォルトのデータベースを選択する

説明

bool mysqli_select_db ( mysqli link, string dbname )

mysqli_select_db() 関数は、 (dbname パラメータで指定して)デフォルト データベースを選択します。これは、link で 指定したデータベース接続に対してクエリを実行する際に使用されます。

注意: この関数は、接続のデフォルトデータベースを変更する際にのみ使用します。 デフォルトデータベースは、mysqli_connect() の 4 番目の引数でも指定可能です。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

参考

mysqli_connect() そして mysqli_real_connect()

例 1. オブジェクト指向型

<?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();
?>

例 2. 手続き型

<?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.