How to convert localhost database to json using php

In this article we will discuss about local host database. There are two type of localhost database. Xampp Server Localhost and cPanel local host.

Xamp \ Wampp Server local host

In xampp\Wampp server local host we need to download software which are xampp or wampp.

  1. Xampp Server download link (Click Here)

2. Wampp Server download link (Click here)

So i recommend you to install Xamp server because its open source and its size is less than wampp server.

After download, Install software and than open software.\

  1. Start Apache and MySql in xampp server.

2. Click on MySQL Admin.When you can click on admin automatically run your chrome with localhost link.The link is

http://localhost/phpmyadmin/

3. Create Database on your xampp server and then create table.

4. Insert the name, type , value length of columns and also make primary key which is automatically increase.

5. Than Go to Xampp server installation location, find htdocs folder in xampp server folder. for example

( C:\xampp\htdocs ).

6. Create php file and mention the name of that file and then right click on that file, open that file in chrome.

7. Edit the link of file which is open in chrome.

Remember If you can create a folder and then create a php file, then you can edit the folder link.

Before our editing link is file:///C:/xampp/htdocs/android/insertdata.php after editing link is http://localhost/android/insertdata.php. Replace the name from file to htdocs .

8. Now Paste this code in php file.

<?php 

$host="localhost";
$DatabaseName= "databaseName";
$userName= "root";
$Password="";


$SqlQuery= "Select *from tablename;";

$conn= new mysqli($host,$userName,$Password,$DatabaseName) or die("cannot write");


$result = mysqli_query($conn,$SqlQuery);
$number_of_rows= mysqli_num_rows($result);

$tem_array = array();
if($number_of_rows>0)
{
while ($row = mysqli_fetch_assoc($result))

{
    $tem_array[]=$row;
}
}

echo json_encode($tem_array);
?>

9. Now last step is to change database name and table name.

10. Now reload the link which is open while opening file in Chrome/Mozilla.

If you have any issue watch my video. Thank you for reading my article.

Leave a Reply

Your email address will not be published. Required fields are marked *