Connect MYSQL Database using PHP
In this PHP tutorial, you are going to learn about how to connect MySQL Database to our program using Hypertext Preprocessor.
Connect MySQL Database to our program using PHP
MySQL is an Oracle-backed open source relational database management system based on Structured Query Language (SQL). MySQL can run on virtually all platforms, including Linux, UNIX and Windows. MySQL is used in a wide range of applications and is the most commonly used database among others.
Connecting a database is a very first task when we have to perform operations on Database. We can connect to a database simply by using mysqli_connect( ) method.
Real life applications of connecting a database to our program:
Whenever we require to store the data we need a database to store the user data. In such cases, we create a database and save our data there. To do so we have to first connect our database with the program using the below mentioned command.
In case of signup form, we have to save the user data to do so a database is required.
We have to pass 4 parameters to the mysqli_connect( ) method that are .
- Server Name: Name of the server on which we are working.
- User Name: Name of the user you can find that in XAMPP settings.
- Password: Password to the user.
- Database Name: Name of the Database you are working on.
Now, I have given a small example to show how to connect to a database.
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "project"; $q = mysqli_connect($servername,$username,$password,$dbname); if(isset($q)) echo "___database connected___"; else die("database not connected".mysqli_connect_error()); ?>
- Here, I used localhost as my server.
- Also, I used root as my user.
- Here, the project is my database name.
- Used a simple if statement to show whether Database is connected or not.
You can also read: