0
<?php
include '../connection.php';

$sql="SELECT userid,name,batch FROM dbusers";
$results=mysql_query($sql) or die("Cannot execute query");
$count=mysql_num_rows($results);
$arr=array();
for($i=0; $i < $count; $i++){
$rows=mysql_fetch_array($results);
//What to put here ?
}
json_encode($arr);

?>

这是我的 php 代码。我想问在 for 循环中放什么,以便我可以在 php 中创建一个数组数组。内部数组将有用户 ID、名称和批次作为其元素。

4

2 回答 2

3

放什么在这里?

$arr[] = $rows;

完整代码

<?php
include '../connection.php';

$sql="SELECT userid,name,batch FROM dbusers";
$results=mysql_query($sql) or die("Cannot execute query");
$count=mysql_num_rows($results);
$arr=array();
for($i=0; $i < $count; $i++){
    $rows=mysql_fetch_array($results, MYSQL_ASSOC);//use MYSQL_ASSOC so you wouldn't have duplicate data
    $arr[] = $rows;
}
$json = json_encode($arr);

?>
于 2013-01-10T05:43:48.473 回答
0
<?php
include '../connection.php';

$sql     = "SELECT userid,name,batch FROM dbusers";
$results = mysql_query($sql) or die("Cannot execute query");
$arr     = array();

while($rows = mysql_fetch_assoc($results)){
    $arr[] = $row;
}
echo json_encode($arr);

?>

尝试这个。

于 2013-01-10T05:44:33.610 回答