To Connect Mysql Database in PHP :
PHP mysqli_connect() function is used to connect with MySQL database. It returns resource if connection is established or null.
Below code snippet will allow you to connect to MySQL database in PHP.

Example :

 <?php
                               
   $dbhost = 'localhost';  // database server ip address
   $dbuser = 'username';   // database username
   $dbpass = 'password';   // database password
   // Create connection
   $conn = new mysqli($dbhost, $dbuser, $dbpass);
   if ($conn->connect_error)
   {
     die('Unable to connect to MySQL database: ' . $conn->connect_error);
   }
   echo 'Connected to MySQL database Successfully';
   mysqli_close($conn); // Closing connection
   
  /*

  Output :

  Connected to MySQL database successfully
   
  */
  
 ?>

  
About Me
Rahul Yadav

Rahul Yadav

Technical Architect

Kuala Lumpur, Malaysia

Connect Me:     

I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

Tag Cloud