Selecting MySQL Database from the Command Prompt

There be a situation when there are different databases present and you have to select one of them to work on. So after MySql connection you can do the following things:

Selecting MySQL Database from Command Prompt:

This is very simple to select a particular database from mysql> prompt. You can use SQL command use to select a particular database.

[bha size='468x60' variation='01' align='none']

Example:

Here is an example to select database called  “DATABASE TUTORIALS”:

[root@host]# mysql -u root -p

Enter password:******

mysql> use DATABASE TUTORIALS;

Database changed

mysql>

As you select “DATABASE TUTORIALS” database and all the subsequent operations will be performed on DATABASE TUTORIALS.

PHP Script:

PHP provides function mysql_select_db to select a database. It returns TRUE on success or FALSE on failure.

Syntax:

bool mysql_select_db( db_name, connection );

CommandDetail
db_nameMySQL Database name is to be selected by the user
connectionOptional – then last opened connection by mysql_connect will be used.

Example:

How to select a database.

[php]

<!--?php
$dbhost = 'localhost';
$dbusername = 'guest';
$dbpassword = 'guest123';
$con = mysql_connect($dbhost, $dbusername, $dbpassword);
if(! $con )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( ' DATABASE TUTORIALS' );
mysql_close($con);
?-->

[/php]

Related Article

Leave a Comment