<?php
/* データベース test に接続します */
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* 接続をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* 変数 a を設定します */
$mysqli->query("SET @a:=1");
/* すべてをリセットし、新しいデータベースを選択します */
$mysqli->change_user("my_user", "my_password", "world");
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("デフォルトデータベース: %s\n", $row[0]);
$result->close();
}
if ($result = $mysqli->query("SELECT @a")) {
$row = $result->fetch_row();
if ($row[0] === NULL) {
printf("変数 a の値は NULL\n");
}
$result->close();
}
/* 接続を閉じます */
$mysqli->close();
?>
<?php
/* データベース test に接続します */
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
/* 接続をチェックします */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* 変数 a を設定します */
mysqli_query($link, "SET @a:=1");
/* すべてをリセットし、新しいデータベースを選択します */
mysqli_change_user($link, "my_user", "my_password", "world");
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
printf("デフォルトデータベース: %s\n", $row[0]);
mysqli_free_result($result);
}
if ($result = mysqli_query($link, "SELECT @a")) {
$row = mysqli_fetch_row($result);
if ($row[0] === NULL) {
printf("変数 a の値は NULL\n");
}
mysqli_free_result($result);
}
/* 接続を閉じます */
mysqli_close($link);
?>
デフォルトデータベース: world
変数 a の値は NULL