Creating a static website is an easy task because you are hard coding the contents into an HTML page. But in the case of dynamic (php) websites, it’s a bit different. You have to display different values on the same position for different users.
To do this, we use databases that are like huge registers with a large number of pages. We store different values of the same variable in tables. And when we need that values, we fetch them using some programming languages and then display them on our web page.
What is MySQL database?
The term database means a collection of related tables that are used to store some information. We use these tables to store variable values in the form of rows and columns. There are many database systems available out there to use like Oracle, MongoDB, NoSQL, etc.
The most commonly used database system for normal operations is MySQL. This database allows us to perform all basic operations like insertion, updation, deletion and reading the data from tables. This model enables us to do these tasks without much complex programming skills.
How to connect MySQL Database to PHP?
Most commonly MySQL is used in combination with PHP. Handling queries using core PHP or laravel Php is quite easy for a new learner. You just have to create a connection with MySQL in your main .php page or you can create a separate file to reuse it.
Let’s see how it’s done.
- Step 1: Create a PHP file and name it “connection.php”.
- Step 2: Paste the following code into the file and save.
<?php
$conn = mysqli_connect('localhost' , 'root' , '' , '[db_name]');
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
Code Explaination:
Tags: The php works on the same principle as html or css. It also starts and ends with specific tags <?php is the starting tag and ?> is the ending tag.
Variables: To define a variable, we use $ followed by the variable name. $conn is the variable name in this code to store connection information.
Functions: In php, functions work exactly similarly to all other programming languages. We can use a function by typing the name of the function and then placing small brackets without any space. Just like function_name(). Here in this code, mysqli_connect() is the function that is used to create a connection between MySQL database and php code.
Function Parameters: To use a function, we must give some input value to it. So it can process that data and give us output accordingly.
- localhost: It is the name of our server. (Keep it default)
- root: It is the username of the server. (If you are admin, keep it default)
- Password: It is the password for the user account. (Keep this blank if you are admin)
- Database name: It is the database name that we will use on our website.
Create database connection to multiple php pages
Creating database connections individually on all pages is not recommended. You can use a single connection file like the above on multiple pages. Just add the following code to all pages where you want to create a connection.
<?php
include('connection.php');
?>
OR
<?php
include 'connection.php';
?>
It will work as it is coded on the same page. Just like you use other cdns or links to make connections with stylesheets and JavaScripts.
Conclusion
I hope in this article, your all doubts are cleared. Now you can practice different methods to connect your database with your php pages. Always double-check your connection file for the server name, username and database name. Because most of the errors come from this type of mistake.