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. Use SQL commands to select the specified database.

The following example selects the database mysql:


After executing the above command, you have successfully selected the mysql database, and subsequent operations will be executed in the mysql 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 mysql_select_db to select a database. The function returns true after successful execution, false otherwise.

bool mysql_select_db( db_name, connection );
Parameter Description
db_name Required. Specifies the database to be selected.
connection Optional. Specifies the MySQL connection. If not specified, the previous connection is used.

The following example shows how to use the mysql_select_db function to select a database:

  1. <html>
  2. <head>
  3. <metacharset="utf-8">
  4. <title>选择 MySQL 数据库</title>
  5. </head>
  6. <body>
  7. <?php
  8. $dbhost ='localhost:3036';
  9. $dbuser ='guest';
  10. $dbpass ='guest123';
  11. $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  12. if(! $conn )
  13. {
  14. die('连接失败: '. mysql_error());
  15. }
  16. echo '连接成功';
  17. mysql_select_db('RUNOOB');
  18. mysql_close($conn);
  19. ?>
  20. </body>
  21. </html>
operation result:


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326492411&siteId=291194637