How can we select a MySQL database by using PHP script?


As we know that PHP provides us the function named mysql_select_db to select a mysql database.

Example

To illustrate this we are selecting a database named ‘Tutorials’ with the help of PHP script in the following example −

<html>
   <head>
      <title>Selecting MySQL Database</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'guest';
         $dbpass = 'guest123';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully';
         mysql_select_db( 'TUTORIALS' );
         mysql_close($conn);
      ?>
   </body>
</html>

Updated on: 22-Jun-2020

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements