MySQLi Connection with PHP Script

PHP provides mysqli_connect() function to open a database connection. This function takes six parameters and returns the successful connection as an object. If the connection failed to establish, then the function returns the boolean value False.

Example :-

$con = new mysqli($host, $username, $passwd, $dbname);

You can disconnect from the MySQLi database using the MySQLi close function.

Statement to close connetion :-

$con -> close();

PHP example of connecting to a MySQLi server –

   
      <html>
   <head>
      <title>Connecting to MySQLi Server</title>
   </head>
   <body>
      <?php
         $host = "localhost";
   $username  = "root";
   $password = "password";
   $dbname = "mydb";
   $con = new mysqli($host, $username, $password, $dbname);
   if($con->connect_errno){
      print("Connection not established. ");
   }else{
      print("Connection Established Successfully");
   }
   $con -> close();
      ?>
   </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *