Check Teer Result

How to create DB connection and extract data using Mysqli

Data Base Connection and Extract data using PHP and MySqli

In this session, we will learn how to create a MySqli database connection & how to extract data using PHP and MySqli from PHPMyAdmin. We will go through video tutorials and Source Code below as the reference. Full Process - 1) First of all we need a PC/Laptop on windows or Linux. 2) we will do the setting of wamp server on a PC. We create the dynamic website using PHP and MySqli. Now, what is a dynamic website, Website which has to panel (i.e) Frontend for the users which has only viewing rights and the backend where the owner of the site has full rights to update or add a new post? 3) After successfully setting of wamp server we will try to connect our frontend with the database (In Php Myadmin) using MySqli Coding. 4) Before that, we have to create database and tables in our database with whom our frontend will form a uniform connection.



See below for full source code:- 

Now we describe our MySqli Coding step by step. Step 1 [$connect = mysqli_connect("localhost","root","","blog"); ] - We have created a connection between the page of the website with PhpMyadmin with the database "blog". Step 2 [$insert_query = "SELECT * from posts";] - WE are saying to select all the columns which are present in the table post. Step 3 [$run_query = mysqli_query($connect, $insert_query);] - we now running our query and saving it as $run_query. Step 4 [while($extract = mysqli_fetch_array($run_query)){] - we are running a while loop here so that the query can run till the selection of columns ends in the table posts. Step 5 [ $title = $extract["post_title"];] - we created a variable $title and save the value of post_title in it similarly we do the Step 6 and finally we print the output.

<?php
$connect = mysqli_connect("localhost","root","","blog");

$insert_query = "SELECT * from posts";
$run_query = mysqli_query($connect, $insert_query);
while($extract = mysqli_fetch_array($run_query)){
$title = $extract["post_title"];
$description  = $extract["post_desc"];
echo "<h1>$title</h1>";
}
?>

So after the creation of a connection between frontend and PHP MyAdmin, we are going to form a relation between the tables in a database "blog" named as "posts". We started extracting information from our database. We can do this both in Offline and Online Mode. In Offline Mode we have to download wamp server in our PC and after that, we have to save our website folder in www directory inside the www folder. we may run the source code by this way in an address bar of any browser LOCALHOST/name of the folder saved inside the www folder/. we can save our main page as index.php or index.html. we will work on MYSQLI (Latest version of Mysql). In Online mode, we should upload our main folder to the online server say Godaddy or Bigrock through FTP Software. On this case we will make some changes in our database connection such as  [$connect = mysqli_connect("localhost","username","password","blog"); ] username and password will be replaced with the respective username given by GoDaddy or whatever we have selected during setting of web server. DNS of the selected domain should be pointed to our web server to make our site live on the internet and we are ready to browse our website.

Video Tutorials:- 

We will go through video tutorials for the session (How to establish database connection using PHP and MySqli)





EmoticonEmoticon