Technology Blog





Thursday, October 20, 2011

Facebook, Twitter like JSON feeds with PHP, JavaScript, Ajax, jQuery,MySQL

When you go to Facebook or Twitter, The first thing you notice is that the page is loaded and within seconds the status feeds will be displayed. That is because the page is loaded first and after that it sends an ajax request to another page and gets back the feeds in the JSON format and display it using JavaScript & jQuery.

We will demonstrate this with a simple posts from mysql table using PHP and JavaScript...

1 ) Create a mysql database and table 'posts' with the following schema.

`post_id` int(5) NOT NULL AUTO_INCREMENT,
`user_id` int(5) NOT NULL,
`user_name` varchar(50) NOT NULL,
`post` varchar(500) NOT NULL,
PRIMARY KEY (`post_id`)

And insert some rows in the table 'posts'

2 ) create a php page 'json-posts.php' that fetches data from database and display it in the JSON format.


//database connection
$dbName = 'twinfo_demos';
$dbLinkConnection = mysql_connect($dbHost,$dbUserName,$dbPassWord) or die("Couldn't make connection.");
$dbSelected = mysql_select_db($dbName, $dbLinkConnection) or die("Couldn't select database");

//select posts from database
$query="select * from posts order by post_id desc";
$result=mysql_query($query) or die("couldn't select data from table");

//create json format text from posts

//display it to the user
header('Content-type: application/json');
echo "{"posts":".json_encode($json)."}";

The above code will fetch the data from database and gives it in the following JSON format...

"post":"Third Post. Third Post. Third Post.",
"time":"2011-10-18 08:37:53"
"post":"Second Post. Second Post. Second Post.",
"time":"2011-10-18 08:37:23"
"post":"First Post. First Post. First Post.",
"time":"2011-10-18 08:37:23"

3 ) create anothe page 'posts.php' to fetch the posts in JSON format and display it to the user. In that page add the following code to send the ajax request for json data using jquery and display the posts for the user.

<script language="javascript" src="jquery-1.4.4.min.js"></script>
$.getJSON('json-posts.php', function(json) {//to send a ajax request for json data
var output="";
for(var i=0;i<json.posts.length;i++){//for each posts in the json response
output+="<small>posted on "+json.posts[i].time+"</small>";
<div id="posts"></div>

This will display the json posts in user readable format and also useful for giving API to others through JSON. Make use of my previous post to display the time in friendly readable format.

Live Demo | Download Code


  1. this is the very good site to buy social services like instagram followers, likes, comments, share etc. they are also providing same services for facebook, twitter, pinterest, soundcloud, and many other sites, such a good service 24/7 service with money back guarantee.
    Buy Instagram Followers Uk

  2. You can also set Facebook up to require approval of a log in. When someone (hopefully you) attempts to log in a text message with a verification code is sent to you. hackear cuenta de facebook

  3. Get social media services at cheap rates buy Instagram followers and likes uk at the lowest price what you need if you will get all things at one place, secure payment via PayPal, money-back guarantee 24/7 customer support. Buy Instagram Followers uk Buy Instagram Likes Uk

  4. This can generally enhance the possibility of profitable, especially if two or more reels are held. Modern slot machines are managed by EPROM computer chips and, in giant casinos, coin acceptors have turn into out of date in favor of invoice acceptors. These machines and their invoice acceptors are designed with superior anti-cheating and anti-counterfeiting measures and are difficult to defraud. Early computerized slot machines have been generally defrauded through the use of of} dishonest gadgets, such as the "slider", "monkey paw", "lightwand" and "the tongue". Many of those old dishonest gadgets have been made by the late Tommy Glenn Carmichael, a slot machine fraudster who reportedly stole over $5 million. In the fashionable day, computerized slot machines are absolutely deterministic 카지노사이트 and thus outcomes may be generally efficiently predicted.