Table of contents
Select the MySQL database from the command prompt window
Select MySQL database using PHP script
After you connect to the MySQL database, there may be multiple databases that can be operated, so you need to select the database you want to operate.
Select the MySQL database from the command prompt window
A specific database can be easily selected in the mysql> prompt window. You can use SQL commands to select specific databases.
example
The following example selects the database RUNOOB:
[root@host]# mysql -u root -p
Enter password:******
mysql> use RUNOOB;
Database changed
mysql>
After executing the above command, you have successfully selected the RUNOOB database, and all subsequent operations will be executed in the RUNOOB database.
Select MySQL database using PHP script
PHP provides the function mysqli_select_db to select a database. The function returns TRUE if executed successfully, otherwise returns FALSE.
grammar
mysqli_select_db(connection,dbname);
parameter | describe |
---|---|
connection | required. Specifies the MySQL connection to use. |
dbname | Required, specifies the default database to use. |
example
The following example shows how to use the mysqli_select_db function to select a database:
select database
<?php
$dbhost = 'localhost'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
echo '连接成功';
mysqli_select_db($conn, 'RUNOOB' );
mysqli_close($conn);
?>