Article directory
MySQL databaseHow to choose?MySQLselect database command/statement/syntax
MySQL select database
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 the specified database.
Examples
The following example selects the database chenweiliang:
[root@host]# mysql -u root -p Enter password:****** mysql> use chenweiliang; Database changed mysql>
After executing the above command, you have successfully selected the chenweiliang database, and the subsequent operations will be executed in the chenweiliang database.
note:All database names, table names, and table fields are case-sensitive.So you need to enter the correct name when using the SQL command.
Select MySQL database using PHP script
PHP provides the function mysqli_select_db to select a database.The function returns TRUE after successful execution, otherwise returns FALSE.
grammar
mysqli_select_db(connection,dbname);
| 参数 | Description |
|---|---|
| connection | Required.Specifies the MySQL connection to use. |
| dbname | Required, specifies the default database to use. |
Examples
The following example shows how to use the mysqli_select_db function to select a database:
select database
<?
php
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
echo '连接成功';
mysqli_select_db($conn, 'chenweiliang' );
mysqli_close($conn);
?>Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How to choose MySQL database? Selecting Database Commands/Statements/Syntax in MySQL" will help you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-452.html
