如何使用php从mysql表中编码json数组

如何使用php从mysql表中编码json数组

本文介绍了如何使用php从mysql表中编码json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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.





我尝试的项目源代码是

[]

推荐答案




这篇关于如何使用php从mysql表中编码json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 12:09