Popular Posts

There was an error in this gadget

Nov 27, 2011

Get output in JSON format(PHP)

--
-- Table structure for table `test_table`
--

CREATE TABLE IF NOT EXISTS `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(20) NOT NULL,
  `sort_order` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `test_table`
--

INSERT INTO `test_table` (`id`, `title`, `sort_order`) VALUES
(1, 'Article 1', '2'),
(2, 'Article 2', '5'),
(3, 'Article 3 ', '4'),
(4, 'Article 4', '1'),
(5, 'Article 5', '3');


<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('extdb') or die(mysql_error());

$q = mysql_query("SELECT * FROM test_table");
while($e = mysql_fetch_assoc($q))
{
$output[] = $e;   
}
print(json_encode($output));

?>



OUTPUT:

[{"id":"1","title":"Article 1","sort_order":"2"},{"id":"2","title":"Article 2","sort_order":"5"},{"id":"3","title":"Article 3 ","sort_order":"4"},{"id":"4","title":"Article 4","sort_order":"1"},{"id":"5","title":"Article 5","sort_order":"3"}]

No comments:

Post a Comment