How to choose MySQL database? Select database command/statement/syntax in MySQL

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
connectionRequired.Specifies the MySQL connection to use.
dbnameRequired, 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);
?>
 

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top