php MySQL database selection

After you connect to the MySQL database, there may be multiple databases can operate, 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 use SQL commands to select the specified database.

Examples

The following examples selected database RUNOOB:

 

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

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

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


MySQL database using PHP script selection

PHP provides functions mysqli_select_db to select a database. After performing the function returns TRUE if successful, otherwise FALSE.

grammar

mysqli_select_db(connection,dbname);

 

parameter description
connection essential. MySQL provisions connection to use.
dbname Necessary, to use the default database specified.

Examples

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

? < PHP
 $ dbhost = 'localhost: 3306';   // MySQL server host address 
$ dbuser = 'root';             // MySQL username 
$ dbpass = '123456';           // MySQL username and password 
$ conn = mysqli_connect ( $ dbhost , $ dbuser , $ dbpass );
 IF (! $ conn )
{
    Die ( 'connection failed:'. mysqli_error ( $ Conn ));
}
echo '连接成功';
mysqli_select_db($conn, 'RUNOOB' );
mysqli_close($conn);
?>

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11163661.html