How to generate .json file with PHP?

Posted by Srinivas Tamada on Stack Overflow See other posts from Stack Overflow or by Srinivas Tamada
Published on 2010-03-18T06:39:36Z Indexed on 2010/03/18 6:41 UTC
Read the original article Hit count: 194

Filed under:
|
|
CREATE TABLE Posts
{
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(200),
url VARCHAR(200)
}

json.php code

<?php
$sql=mysql_query("select * from Posts limit 20");
echo '{"posts": [';
while($row=mysql_fetch_array($sql))
{
$title=$row['title'];
$url=$row['url'];
echo '

{

"title":"'.$title.'",

"url":"'.$url.'"

},'; 
}
echo ']}';

?>

I have to generate results.json file.

© Stack Overflow or respective owner

Related posts about php

Related posts about php5