Latest web development tutorials

MySQL Select Database

After you connect to the MySQL database, it may have multiple databases that can be manipulated, so you need to select the database you want to operate.


Select the MySQL database from the command prompt window

In the mysql> prompt window can be very simple to select a specific database. You can choose to specify the database using SQL commands.

Examples

The following examples selected database w3big:

[root@host]# mysql -u root -p
Enter password:******
mysql> use w3big;
Database changed
mysql>

After executing the above command, you have successfully selected w3big database, the database will be executed in w3big in subsequent operations.

Note: All database names, table names, table fields are case sensitive. So when you use SQL commands need to enter the correct name.


Select the MySQL database using PHP script

PHP provides functions mysql_select_db to select a database. After the successful implementation of the function returns TRUE, otherwise it returns FALSE.

grammar

bool mysql_select_db( db_name, connection );
parameter description
db_name Required. To select the specified database.
connection Optional. Provisions MySQL connection. If not specified, the last connection.

Examples

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

<html>
<head>
<meta charset="utf-8"> 
<title>选择 MySQL 数据库</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('连接失败: ' . mysql_error());
}
echo '连接成功';
mysql_select_db( 'w3big' );
mysql_close($conn);
?>
</body>
</html>