Special Characters to Use in PHP Expression Test Patterns
Make MySQL Database Changes with the ALTER Query
PHP Syntax

How to Select a MySQL Database

If you don’t select the MySQL database in the connect function, you can select the database by using the mysqli_select_db function. You can also use this function to select a different database at any time in your script. The format is

mysqli_select_db($cxn,"databasename")
      or die ("message");

If you’re using the mysql functions, rather than the mysqli functions, you must select the database in a separate function, using mysql_select_db.

Fill in the following information:

  • *cxn: The variable that contains the connection information.

  • databasename: The name of the database.

  • *message: The message that’s sent to the browser if the database can’t be selected. The selection might fail because the database can’t be found, which is usually the result of a typo in the database name.

For instance, you can select the database Customer with the following statement:

mysqli_select_db($cxn,"Customer")
      or die ("Couldn't select database.");

If mysqli_select_db can’t select the database, the script stops running and the message Couldn't select database. is sent to the browser.

The database stays selected until you select a different database. To select a different database, just use a new mysqli_select_db function statement.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Build an About Page on a PHP Template
PHP Statements
How to Choose the Data for Your MySQL Database
Changes for the ALTER Query in PHP and MySQL Web Development
WHERE Clause Format for PHP and MySQL Web Development
Advertisement

Inside Dummies.com