How To Create Database Connection Using Php And Mysql
But before we start there are some certain materials required to run PHP, since we are all know that PHP is a server-side scripting language. We need a server to run PHP.In this tutorial we are working offline, so there are some basic requirements
Requirements
These are some of the list of required item to follow along we the tutorials, If you do not have any of the listed materials you can search the web to get them as roll on. Here are the required items as listed below:
- PHP Apache Server
- MySQL Server
- Code Editor eg Sublime Text,Notepad++,Brackets etc
NOTE: For the PHP Apache and MySQL server you can download already compiled software that comprises of the two Server eg Xampp Server,Wampp Server etc.
STEPS
1.Install the coder that you have downloaded and also the Server(Xampp,Wampp etc).With these installed then you are ready to go.
2.We are going to be using Xampp server in these tutorials,before you start you will run Xampp server and also a browser
3.After installation run the Xampp server,take glance at how it look like while running.
4.Open your browser and type localhost/phpmyadmin to create a database.Phpmyadmin looks like the below image.
5.Click on new to create database.For this tutorial,we will be creating a database name Basis.After clicking the new button a page will pop-up enter your database name and click Create and the database will be created as shown below.We need to create tables after creating database but for the case of this tutor,we are stoping at database creation.
6.After carrying out step 5 correctly,open your coder and create a page db.php and type the following codes as shown below.
<?php $db['db_host'] = "localhost"; $db['db_user'] = "root"; $db['db_pass'] =""; $db['db_name'] = "basic"; foreach($db as $key => $value){ define(strtoupper($key),$value); } $connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if($connection){ echo "We are connected"; } ?>Note:With the code typed above,you are now connected to the Database called basic