【问题标题】:How to get Joomla users data into a json array [closed]如何将 Joomla 用户数据放入 json 数组 [关闭]
【发布时间】:2012-11-28 03:11:30
【问题描述】:
$sql = "SELECT * FROM `jos_users` LIMIT 0, 30 ";

$response = array();
$posts = array();
$result=mysql_query($sql);

while($row=mysql_fetch_array($result)) 
{ 
    $id=$row['id']; 
    $id=$row['name']; 

    $posts[] = array('id'=> $title, 'name'=> $name);

} 

$response['jos_users'] = $posts;

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

我想获取 json 文件的用户 ID 和名称。我认为 id 的代码有误。谁能纠正它?

【问题讨论】:

  • 有什么问题?你得到什么错误?
  • bluewingholidays.com/results.json ..当我更新 php 文件时什么都没有
  • 1) 你确认你连接mysql成功了吗? 2) 确认查询运行成功? 3) 确认$posts 被填充? 4)results.json 是可写的并且 fopen 成功了吗?您在该代码中有很多假设,并且在错误处理方面绝对为零。
  • 为什么要将整个响应写入文件?
  • 准确描述你的目标是什么,举个目标json数组的例子!

标签: php mysql json


【解决方案1】:

您正在覆盖 $id 变量,然后您没有使用它...

$title$name$id 变量似乎一团糟。

试试这个:

 <?php 
$sql = "SELECT * FROM `jos_users` LIMIT 0, 30 ";

$response = array();
$posts = array();
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) 
{ 
$id=$row['id'];  //change here
$name=$row['name']; //change here

//change variables here
$posts[] = array('id'=> $id, 'name'=> $name);

} 

$response['jos_users'] = $posts;

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);


?> 

【讨论】:

  • 为什么 OP 应该尝试这个?
  • 不要只是剪切和粘贴一段代码并更改两个字母。指出您进行更改的位置,但在书面回复中并通过代码中的内联注释。但除此之外,很好的收获。
  • 我编辑了我的答案。好像有些变量不对。
猜你喜欢
  • 1970-01-01
  • 2014-03-12
  • 2012-12-27
  • 2021-10-02
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 2020-03-17
相关资源
最近更新 更多