You can create json file using PHP and Mysql. You can get output as json format using PHP and Mysql. Before you want to know that, first you should know what is json? and what is the us of json?
JSON expands JavaScript Object Notation. It is one of the data interchange format. You can easily read and write data through JSON file.
The object of JSON should be like this,
Now you know something about JSON files. So we are going to know how to get output as JSON format using php and mysql.
If you want to create json file, then you have to add header for json file. It is like below
The above example explain how to create json file on multidimensional array using PHP and Mysql.
where,
array() - create array variable.
array_push() - insert values into array
json_encode() - encode the values and returns JSON representation of values
The above example will give output as json file. It should be like this:
JSON
JSON expands JavaScript Object Notation. It is one of the data interchange format. You can easily read and write data through JSON file.
It is language independent. So you can retrieve data from it using any familiar languages like PHP. It should be array format, object format and both array and object fotmat.
The array format of Json file should be like this,
[
{
'tutorial':'PHP',
'link':'http://www.phponwebsites.com/p/php.html'
},
{
'tutorial':'Mysql',
'link':'http://www.phponwebsites.com/p/mysql.html'
},
{
'tutorial':'htaccess',
'link':'http://www.phponwebsites.com/p/htaccess.html'
}
]
{
'tutorial':'PHP',
'link':'http://www.phponwebsites.com/p/php.html'
},
{
'tutorial':'Mysql',
'link':'http://www.phponwebsites.com/p/mysql.html'
},
{
'tutorial':'htaccess',
'link':'http://www.phponwebsites.com/p/htaccess.html'
}
]
The object of JSON should be like this,
{
'tutorial':'PHP',
'link':'http://www.phponwebsites.com/p/php.html'
}
'tutorial':'PHP',
'link':'http://www.phponwebsites.com/p/php.html'
}
Get output as json format using PHP and Mysql
Add header to create json file using PHP and Mysql
If you want to create json file, then you have to add header for json file. It is like below
header('Content-Type: application/json');
Where json indicate this is json file
Add content to json file using PHP
Then you need to add contents to your json file as you want as. Retrieve values from query result using mysql_fetch_array. Suppose you have more values in a variable. Then you need to store all values in array. Consider the following PHP code.
<?php
header('Content-Type: application/json');
$linklist=array();
$link=array();
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('quicksai_quick') or die(mysql_error());
$qr=mysql_query("SELECT * FROM `links` ") or die(mysql_error());
while($res=mysql_fetch_array($qr))
{
$link['tutorial']=$res['tutorial'];
$link['url']=$res['url'];
array_push($linklist,$link);
}
//print_R($linklist);
echo json_encode($linklist);
?>
header('Content-Type: application/json');
$linklist=array();
$link=array();
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('quicksai_quick') or die(mysql_error());
$qr=mysql_query("SELECT * FROM `links` ") or die(mysql_error());
while($res=mysql_fetch_array($qr))
{
$link['tutorial']=$res['tutorial'];
$link['url']=$res['url'];
array_push($linklist,$link);
}
//print_R($linklist);
echo json_encode($linklist);
?>
The above example explain how to create json file on multidimensional array using PHP and Mysql.
where,
array() - create array variable.
array_push() - insert values into array
json_encode() - encode the values and returns JSON representation of values
The above example will give output as json file. It should be like this:
[
{'tutorial':'PHP', 'link':'http://www.phponwebsites.com/p/php.html'},
{'tutorial':'Mysql', 'link':'http://www.phponwebsites.com/p/mysql.html'},
{'tutorial':'htaccess','link':'http://www.phponwebsites.com/p/htaccess.html'}
]
Now you can get output as json file. Then you should know how to retrieve values from json file using PHP.
Related Post:
Create Excel file using PHP and MySQL
PHP : Create XML file
Create XML Sitemap dynamically using PHP and MySQL
Create RSS Feed Dynamically using PHP and MySQL
PHP : Parse data from RSS Feed using SimpleXML
Read data from JSON file using PHP
Parse data from XML file using PHP
Related Post:
Create Excel file using PHP and MySQL
PHP : Create XML file
Create XML Sitemap dynamically using PHP and MySQL
Create RSS Feed Dynamically using PHP and MySQL
PHP : Parse data from RSS Feed using SimpleXML
Read data from JSON file using PHP
Parse data from XML file using PHP