Connecting to database

The most important things to learn is how to connect to mysql database using php.

We are going to use the mysqli follow the steps below.

1. Create a php file for this tutorial I’ll be using “config.php” to do the connection. And also we will assume that you created a database on your phpmyadmin. For this tutorial my database name is “db_test” and a table named “tbl_test” that contain 4 fields which are “id, name, age, gender”.

mysql & php

2. Okay! so we are ready to write a PHP code to connect to our database. We will start by writing this below

At this moment we are now connected to our database. If you see some alien data that is showing on your browser don’t panic it is normal.

Now we are going to fetch some data from database. But first we will going to insert some dummy data to our table which is tbl_test

Now we are ready to do the fetching of data. Fist we need to do a query to the database to let mysql knows what data are we going to fetch from the database. And we are going to use “fetch_object()” to fetch all the data that mysql return base on our sql query. And do looping if our data is more than 1.