MySQLi - Select DB
语法
bool mysqli_select_db ( mysqli $link , string $dbname )
定义和用法
用于选择数据库查询的默认数据库。
示例
试试下面的例子 −
<?php $servername = "localhost:3306"; $username = "root"; $password = ""; $dbname = "TUTORIALS"; $tmp = NULL; $conn = new mysqli($servername, $username, $password, $dbname); if (!$conn->real_connect($servername, $username, $password, $dbname)) { die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); } echo 'Success... ' . mysqli_get_host_info($conn) . " "; if ($result = $conn->query("SELECT DATABASE()")) { $row = $result->fetch_row(); printf("Default database is %s. ", $row[0]); $result->close(); } $conn->select_db("test"); if ($result = $conn->query("SELECT DATABASE()")) { $row = $result->fetch_row(); printf("Default database is %s. ", $row[0]); $result->close(); } $conn->close(); ?>
上述代码的示例输出应该是这样的 −
Success... localhost:3306 via TCP/IP Default database is tutorials. Default database is test.