Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
<?php
$db = new PDO('mysql:host=localhost:3306;dbname=DB1;', 'user1','123456');
$sql = 'SELECT * FROM events';
$out = array();
foreach($db->query($sql) as $row) {
$out[] = array(
'id' => $row->title,
'title' => $row->name,
'url' => $row->url,
'class' => $row->class,
'start' => $row->start . '000',
'end' => $row->end .'000'
 );
 }

echo json_encode(array('success' => 1, 'result' => $out));
exit;
?>

Table Structure

Field Type Null Key Default Extra
id int(5) NO NULL
title text NO NULL
url text NO NULL
class text NO NULL
start datetime NO NULL
end datetime NO NULL

It displays output as

{"success":1,"result":        [{"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"},    {"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"}]}

i want to print data in tables instead of null

Thank You.

share|improve this question
    
Are you sure your query is returning something? Try executing the query directly on the database. Also try print the $row variable to see its contents – William Janoti Feb 22 '15 at 15:16
    
yes when i execute query it returns values. But i try to use in php it shows null. Here i include my source please help me sadakpramodh.esy.es/json_project.zip – sadakpramodh Feb 23 '15 at 1:26

You are not querying your rows the proper way.

Instead of :

'id' => $row->title,

use

'id' => $row['title'],

for more information, see example 1 in the official documentation

share|improve this answer
    
Thank you for your answer but no change in my output. Here my files are sadakpramodh.esy.es/json_project.zip – sadakpramodh Feb 23 '15 at 1:27
    
what do you have when you do a var_dump($row) – A.D. Feb 23 '15 at 14:08

not tried this but you could change this:

foreach($db->query($sql) as $row) {

to

foreach($db->query($sql) as (object)$row) {

... which might allow the syntax used to access records.

share|improve this answer
    
Thank you RamRaider it doesn't change my output please checkout my project here sadakpramodh.esy.es/json_project.zip – sadakpramodh Feb 23 '15 at 1:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.