如何将Joomla用户数据转换为json数组

时间:2012-12-10 16:54:29

标签: php mysql json

$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);

我想将用户ID和名称提取到json文件。我认为id做错了code.can有人纠正了吗?

1 个答案:

答案 0 :(得分:-2)

您正在覆盖$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);


?>