How To Connect MySQL with PHP

How To Connect MySQL with PHP

MySQL is a very popular open-source relational database management system (RDBMS).

PHP(Hypertext Preprocessor) is a server scripting language and a powerful tool for making dynamic and interactive Web pages.

Steps for connecting the database(MySQL) in PHP:

  1. To run php code you need a server which is provided by XAMPP. So install it first.

  2. Then open the XAMPP control panel and start "APACHE" and "MySQL".

  3. Then Click on the Admin of MySQL row.

  4. This Interface will Open Up.

  5. Create a new database by clicking on the "new" are left corner

  6. Name it and configure it as per your schema.

  7. Now in your project create a 'connect'(you can use any name) file with the extension ".php"

  8. Then use the mysqli_connect () method by passing the appropriate parameter

     <?php
         $connect = mysqli_connect("localhost","root","","voters") or die("connection failed");
         if($connect){
             echo "Connected";
         }
    
         else{
             echo "Not Connected";
         }
     ?>
    
  9. Save the file.

  10. Now on the browser type "Localhost/Project_Name/connect.php", if it shows connected then you are good to go. Connection is successfully created.