Once you get a connection to the MySQL server, you need to choose a specific database to work with. This is because the MySQL server may have more than one database.
From the command prompt, select MySQL Database :
It is very simple to select a specific database mysql> prompt . To select a specific database, you can use SQL commands.
Example:
The following is an example, selecting the database called TUTORIALS:
[root@host]# mysql -u root -p Enter password:****** mysql> use TUTORIALS; Database changed mysql>
Now the tutorial database has been selected and all subsequent operations will be carried out.
Note: All database names, table names, and field names in tables are case-sensitive. So will have to use prpoer name while giving any SQL command.
Use a PHP script to select a MySQL database:
PHP provides the function mysql_select_db to select a database. Returns TRUE on success, FALSE otherwise.
Syntax:
bool mysql_select_db( db_name, connection );
Example:
The following is an example showing how to select a database.
<html> <head> <title>Selecting MySQL Database - by www.jb51.com</title> </head> <body> <?php $dbhost = 'localhost:3036'; $dbuser = 'guest'; $dbpass = 'guest123'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_select_db( 'TUTORIALS' ); mysql_close($conn); ?> </body> </html>
The above is the detailed content of Simple sample code for connecting to mysql database. For more information, please follow other related articles on the PHP Chinese website!