All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Database Connection In PHP With MySQL In XAMPP Code

Last Updated : Mar 11, 2024

Database Connection In PHP With MySQL In XAMPP Code

In this tutorial we will show you database connection in PHP with MySQL in xampp code, in php we can execute connection with server database so here we are executing connection with xampp server for interact with database.

In database we can do store, edit, update or delete these operations and mainly database used for collects and store all user information. When we need some information from database we retrieve using php because it is a server side language.

Step By Step Guide On Database Connection In PHP With MySQL In XAMPP Code :-

For executing connection with xampp server we need to collect database and server informations.

Then we need use inbuilt ‘mysqli_connect()’ function for creating connection request with those information and when the request gets granted by xampp server then this connection is successfully executed, otherwise it gives error on browser webpage.

Result will display on plain webpage because we are not styled.

<?php
$localhost = "localhost"; #localhost
$dbusername = "root"; #username of phpmyadmin
$dbpassword = ""; #password of phpmyadmin
$dbname = "dbase"; #database name
#connection string
$conn = mysqli_connect($localhost,$dbusername,$dbpassword,$dbname);
    if($conn){
    echo "connected successfully";
    }
    else{
        echo "Error";
    }
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?> tags.
  2. The default file extension for php files is “.php” and php each statements end with ‘;’ semicolon. Between start and end tag we need to define our program in php.
  3. We need to collect server details so we used variables ‘$localhost,$dbusername,$dbpassword,$dbname’ for collecting our xampp informations.
  4. In variable ‘$localhost’ we stored server name and its server name will available on server page top left end. Most of time it’s name will ‘localhost’.
  5. Variable ‘$dbusername’ used for store ‘user name’ its most of time default value also ‘root’, ‘$dbpassword’ variable for store ‘user password’ and ‘$dbname’ variable for store ‘database name’.
  6. Database name will definitely vary because we can give any name for our database when we are creating. If you use above code then create database on xampp server with name ‘dbase’.
  7. Then using ‘mysqli_connect()’ method with parameter of server details we request server for granting permission.
  8. When request grant means its finding database name on xampp server and when it finds connection will success. If the database name not exit or any server details not correct then it throws error on browser webpage.
  9. So we have give correct details of server and database then we can achieve result easily. Then result will printed on our webpage using ‘echo()’ with ‘connection succeed’ string. It also a statement for print any values in php.

Conclusion :-

In conclusion we are able to know how to push key with value to an array using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Before executing this program on browser page, we need to create database on xampp server with name ‘dbase’ and then we can execute connection successful message will display on webpage.

I hope this tutorial on database connection in PHP with MySQL in xampp code helps you and the steps and method mentioned above are easy to follow and implement.