After the establishment of connection with the database server, you have to select the particular database where your tables are associated.This is required because there may be multiple databases and you can work with only one database.

PHP provides a pre-built mysql_select_db() function to select a database. It returns a TRUE value on success and FALSE value on failure.

Syntax of mysql_select_db function

mysql_select_db( database_name, connection )

database_name  - This parameter is required.Database name which you want to select.

connection - This parameter is optional. This is the connection resource or link identifier which is returned by mysql_connect() function on success.If this is not specified then the default is last opened connection of database by mysql_connect function.  


Below is an example for selecting a database

 $database_host = 'localhost:3036';
 $database_user = 'root';
 $database_pass = '';
 $conn = mysql_connect($database_host, $database_user, $database_pass);
 if(! $conn ) {
   die('Could not connect: ' . mysql_error());
   echo 'Connected successfully\n';
 $selected_or_not = mysql_select_db( 'xyz_school' );
 if(! $selected_or_not ) {
   die('Not Selected xyz_school database');
   echo 'Selected xyz_school database';

If the database connection is established and mysql_select_db function return TRUE value then below the line will be printed on the screen and close the connection from the database.

Output: Connected successfully. Selected xyz_school database.

Find us on

Facebook Google Plus Twitter Pinterest YouTube

web hit counter