Select a MySQL database in PHP

Select a MySQL Database: mysql_select_db()

Once you have connected to the mysql database using the mysql_connect () inbuilt function, you need to select a database to use. Again PHP provides simple inbuilt function mysql_select_db() to select a mysql database. This function takes two arguments as explained below.

The Syntax of the valid PHP statement to select a database is as below.

mysql_select_db(“Database”, $conn);

The arguments are explained below.

  • Database: - Database is the mandatory argument. This is the name of the mysql database you want to use for your subsequent queries.
  • $conn: - This is the mysql connection string retrieved using the mysql_connect () function.
    This parameter is not mandatory. If this parameter is not specified, the PHP function will try to use any
    other available connection to mysql server. If no such connection is already established, It will throw an error. All the mysql errors can be retrieved using the mysql_error () function. So most of the time the above code is written like


mysql_select_db (“Database”, $Conn) or die(mysql_error());