如何使用php从mysql表中编码json数组 [英] how to encode json array from mysql table using php

查看:77
本文介绍了如何使用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.





我尝试的项目源代码是

http://sadakpramodh.esy.es/ json_project.zip [ ^ ]

推荐答案

db = PDO(' mysql:host = localhost:3306; dbname = D. B1;'' user1' ' 123456');
db = new PDO('mysql:host=localhost:3306;dbname=DB1;', 'user1','123456');


sql = ' SELECT * FROM events';
sql = 'SELECT * FROM events';


out = array();
foreach(
out = array(); foreach(


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆