How to connect MYSQL Database with php?

Connect MYSQL Database using php step by step?

1) Create a Database.
2) Create a new file in Notepad++ or other text editor that you are using.

Create a Database.

  • Go to your phpmyadmin.
  • Click on Databases at the top.
  • Enter the Database name.
  • Click create button

Create a new file in Notepad++ or other text editor that you are using.

    • Copy and Paste this code in to your text editor like(Notepad++, Sublime or other text editor that you are using )

    <?php

    $hostname=”localhost”;

    $username=”root”;

    $password=””;

    $db_name=”db_conn”; //Here enter your database name

    //Create connection

    $conn=new mysqli($hostname, $username, $password, $db_name) or die(‘could not connect’);

    //Check connection

    if($conn){

    echo”succesfully connected”;

    }

    else{

    echo”Not connected”;

    }

    ?>

    • Then change your database name($db_name=”Here enter your database name”) and save it(Ctrl+S).
    • Go to your browser refresh it
    • Your database will be connected

Leave A Comment?

13 − 7 =