-4
$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 代码错误。有人可以纠正吗?

4

1 回答 1

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


?> 
于 2012-12-10T16:58:18.927 回答