Hàm select_db () PHP mysqli
Ví dụ - Kiểu hướng đối tượng
Thay đổi cơ sở dữ liệu mặc định cho kết nối:
<?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();
?>
Xem ví dụ về phong cách thủ tục ở phía dưới.
Định nghĩa và Cách sử dụng
Hàm select_db () / mysqli_select_db () được sử dụng để thay đổi cơ sở dữ liệu mặc định cho kết nối.
Cú pháp
Phong cách hướng đối tượng:
$mysqli ->
select_db(name)
Phong cách thủ tục:
mysqli_select_db(connection, name)
Giá trị tham số
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
name | Required. Specifies the database name |
Chi tiết kỹ thuật
Giá trị trả lại: | ĐÚNG về thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 5+ |
Ví dụ - Phong cách thủ tục
Thay đổi cơ sở dữ liệu mặc định cho kết nối:
<?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);
?>
❮ Tham khảo PHP MySQLi