How to connect Database in php?

Its very easy to connect php file with you not able to connect database. Its occurring many error on screen. You have lot of try to connect but you even failed here are the perfect place you visit.

First of all, you create connection.php in favorite editor and paste it on XAMPP server in htdoc file.

Now, connection.php run in server with the XAMPP sever or any other.

Its main part of this topic is, you should be create database on phpMyAdmin and create table on it.

  1. Clicking admin button on XAMPP dashboard

2. You are redirected to phpMyAdmin Home page>>click phpMyAdmin button to open database panel. It is look like something like that

3. To create database >> click new button in left corner in the database panel >> Given the name of database

4. Create conn.php file. Now write codes for database connectivity. Check connection successful or not.


$conn = mysqli_connect('localhost','root','','information');

    echo "Database not connected".mysqli_connect_error();
    echo "Database Connect Successful";

5. If error not comes out in above code then else statement will be run and connectivity goes successful. You should be create index.php file for user input with the help of HTML, CSS AND PHP but I am not using css in this tuto. You recommended me then I create for you. In this body section you will write under the form tag. every input and submit button comes under the form tag.

<!DOCTYPE html>
        <title>database conectivity</title>
        <form action="conn.php" method="get">
            <label >Name</label><br>
            <input type="text" name="txtName" required><br>
            <label for="txtUsername">Username</label><br>
            <input type="text" name="txtUsername"><br>
            <label for="txtPass">Password</label><br>
            <input type="password" name="txtPass"><br><br>
            <input type="submit" name="submit" value="submit">

6. Write a code for collecting data from user by using get method. Comment out the else block statement of conn.php file and write query for inserting data into database.

now check the query and connection both are connected or not . If connection is ok then $rs output will be 1 but it isn’t. Then it show 0 for not connecting successfully.


$conn = mysqli_connect('localhost','root','','information');

    echo "Database not connected".mysqli_connect_error();
    //echo "database Connect Successful";

    $name = $_GET['txtName'];
    $username = $_GET['txtUsername'];
    $pass = $_GET['txtPass'];

    $sql = "INSERT INTO learn 

    $rs = mysqli_query($conn, $sql);

            alert('Data Inserted successfuly');


7. Try it for final conclusion. Run the file on xampp server or wampp as your choice. Enter required details in field. Click on submit button and comes pop out alert button of confirmation, click ok.

8. Open your phpmyadmin panel and refresh your panel. Look your entered data.

Leave a Comment

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