Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Code to Fetch SQL Data and Convert to Json API
#1
Introduction:

Today most of the sites are working using API script. After the launch of AngularJS and some other great FrameWork. Developers were started to simplify website development using that Framework. Most of the Frameworks need API to fetch data from Database.  This thread will help you to convert SQL data to JSON API.

First of all you need to connect with your database.

PHP Code:
<?php
$connection 
mysql_connect("localhost","root","");
$database mysql_select_db("DBNAME HERE");
?>

After connecting with the database you have to fetch data from the SQL that you need to convert into the JSON API.

PHP Code:
<?php
include 'config.php';
$query=mysql_query("SELECT * FROM `message`");
while(
$row=mysql_fetch_array($query))
{
 
 $message['message'][] = $row;
}
 echo 
json_encode($message);
?>


The above code will convert the SQL Data from "message" table to JSON API. The "config.php" page will connect the PHP program with the Database.

Now open the page. You can see the result there.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)