ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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=’468×60′ 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.

 

Selecting MySQL Database Using 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 );

 

Command Detail
db_name MySQL Database name is to be selected by the user
connection Optional – then last opened connection by mysql_connect will be used.

Example:

How to select a database.

 

  1. <html>
  2. <head>
  3. <title>Selecting MySQL Hospital Database</title>
  4. </head>
  5. <body>
  6. <?php
  7. $dbhost = 'localhost';
  8. $dbusername = 'guest';
  9. $dbpassword = 'guest123';
  10. $con = mysql_connect($dbhost, $dbusername, $dbpassword);
  11. if(! $con )
  12. {
  13.   die('Could not connect: ' . mysql_error());
  14. }
  15. echo 'Connected successfully';
  16. mysql_select_db( ' DATABASE TUTORIALS' );
  17. ?>
  18. </body>
  19. </html>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!