A simple example of selecting an MySQL database for a connection

  • 2020-10-23 21:14:16
  • OfStack

Once you get a connection to the MySQL server, you need to select a specific database to work with. This is because the MySQL server may have more than one database.
From the command prompt, select the MySQL database:

It is very simple to select 1 specific database mysql > Prompt. Select a specific database and use the SQL command.
Example:

Here is an example of choosing a database called TUTORIALS:


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

Now that you have selected the tutorial database the tutorial database and all subsequent operations will take place.

Note: All database names, table names, and field names in tables are case sensitive. So you will have to use the prpoer name and give any SQL command.
Select the MySQL database using the PHP script:

PHP provides the function mysql_select_db to select 1 database. Returns TRUE on success or FALSE on failure.
Grammar:


bool mysql_select_db( db_name, connection );


Example:

Here is an example of 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>



Related articles: