Thursday, August 15, 2013

Retrieving Data using MYSQL vs JSON


When Dealing with more amount data the most important factor is performance.Below are the two methods which calculate the time  duration of data for display.

When working with more amount data normally we are using MySQL to retrieving and display  data from database, but there is another method to store and retrieve data from database in fast manner which is using “JSON”  file.
Lets see the difference between two methods to store and retrieve the data.
Suppose we have database with 50,000 records.
First of all store the database records  into JSON file.

Store data into JSON file :

mysql_connect("localhost","root","");
 mysql_select_db("test");
 
$query=mysql_query("SELECT * FROM temp ORDER BY id");
 $arr=array();
 while($k=mysql_fetch_object($query))
 {
 $arr[]=$k;
 }
 
file_put_contents("json.json",json_encode($arr));  

Direct request with MySQL:

mysql_connect("localhost","root","");
 mysql_select_db("test");
 
$query=mysql_query("SELECT * FROM temp ORDER BY id");
 while($k=mysql_fetch_object($query))
 {
echo "<pre>";
 
//  Print the data
print_r($k);
}

Retrieve data using JSON file:

</pre>
// Read data from json file
 
$str=json_decode(file_get_contents("json.json"));
 echo "<pre>";
 foreach($str as $s)
 {
print_r($s);
 }

convert mysql data to json :

<?php

mysql_connect('localhost','root',' ')or die("cannot connect"); 
mysql_select_db(fb)or die("cannot select DB");

$sql="SELECT * FROM users ";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){

echo $p[]= $rows['username'];

}
echo json_encode($p);
?>


No comments:

Post a Comment