mysqli::select_db
mysqli::select_db
mysqli_select_db
(PHP 5, PHP 7)
mysqli :: select_db - mysqli_select_db - 选择数据库查询的默认数据库
描述
面向对象的风格
bool mysqli::select_db ( string $dbname )
程序风格
bool mysqli_select_db ( mysqli $link , string $dbname )
选择在对数据库连接执行查询时要使用的默认数据库。
注意
:此功能只能用于更改连接的默认数据库。您可以使用 mysqli_connect()中的第四个参数选择默认数据库。
参数
`link`
仅过程样式:由 mysqli_connect()或 mysqli_init()返回的链接标识符
dbname
数据库名称。
返回值
TRUE
成功或FALSE
失败时返回。
例子
Example #1 mysqli::select
_
db() example
面向对象的风格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row(
printf("Default database is %s.\n", $row[0]
$result->close(
}
/* change db to world db */
$mysqli->select_db("world"
/* return name of current default database */
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"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
/* return name of current default database */
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result
printf("Default database is %s.\n", $row[0]
mysqli_free_result($result
}
/* change db to world db */
mysqli_select_db($link, "world"
/* return name of current default database */
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.
另请参阅
- mysqli_connect() - Alias of mysqli::__construct
- mysqli_real_connect() - 打开到 mysql 服务器的连接
← mysqli::savepoint
mysqli::send_query →