Write an example to establish MySQL database connection using PHP script?


We can use following PHP script to make a MySQL database connection having user ‘guest’ and password ‘guest123’.

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

Updated on: 22-Jun-2020

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements