Build up a connection to a database in PHP and MYSQL - BOOKC -->

Build up a connection to a database in PHP and MYSQL

Sunday, July 16, 2017



When you are visiting some sites you have to register and then login to get use of that site. When you are searching something in a site, it gives the relevant results according to your need. How can this be done? Database connection is the base for these scenarios. In this article, I'll tell you how to create a simple database connection and check whether it's done or not. You need to set up PHP in your computer before going for the coding part. Apache web server and Mysql database should be up and running.

  • First access to phpmyadmin by typing this in the url bar of your browser.
  • Then create a new database. In my case it is called as testdb. It is built up with the following structure. Use the data types for the fields correctly as in normal use. So, use INT for id and VARCHAR for other 3 fields.



  • Create 2 PHP files called user and dbcon
  • PHP file called user is used to store the connection details according to your setting. The code is user.php is like this.
    <?php
     $server = 'localhost';  
     $username = 'root';      
     $password = '';
     $db = 'testdb';
    ?>


Explanation :

Here, localhost is the server that we use to run the code locally in our machines. Username is the name you are using to access phpmyadmin. I have not changed it. Root is the default user. Password means the password you use to access phpmyadmin. Blank is the default password. If you have changed these when configuring PHP, you have to give the correct values otherwise you are unable to run the code in your machine. Then you can see my database name. In this file, server, username, password and db are the variables I used to define my db connection details. You can use your own variable names.
  • Now go for the dbcon file after saving user.php file. I'll put the code hear and then explain it.

<?php

require 'user.php';
$conn=mysqli_connect($server,$username,$password,$db);
if(!$conn){
die('Connection Failed'.mysqli_connect_error());
}
else {
echo "Connected Successfully!";
}
//var_dump($conn);

?>

  • When we starts the dbcon file, we need the parameters  we set in the user file to build up the db connection. So we use a code call require to access it. After typing require you need to give exact path of user.php file. In my case I kept both files in the same folder.
  • Then we use a variable for the connection as conn and assign a code based on mysqli to connect with database using previously defined parameters.
  • Now we are done with building the connection. But how  do we get to know whether the connection is established or not? So now we should include the error messages and also success messages that indicates the status of the connection. In this code, !$conn means the task assigned to $conn is not done. ! sign acts as something is not done. This If condition includes if connection is not established do this thing; output a line called Connection failed. We use die function to end up the connection as soon as and error is occurred with giving the error message. The else part is included with the success message to output it if connection is built up correctly.
  • You can see a code called var_dump($conn). It is used to check whether the db is  connected or not even without using error messages. If the connection is up, array details are printed to the web browser like this. This output will be get by XAMMP users.


  • Now comment out it and check the success with the error and  success messages. If you got connected to the database, output will be like this.

          Try and get the best results! Good luck!