我有一个sql表测验(级别,数据,问题,选项1,选项2,选项3,选项4,答案); php代码
$link=mysql_connect('localhost','root','');
mysql_select_db('sencha_db',$link);
$query="Select * from quiz where level='$Level'";
$res=mysql_query($query);
while($row = mysql_fetch_row($res))
{
$q[]=$row[2];
$op1[]=$row[3];
$op2[]=$row[4];
$ans[]=$row[5];
}
$response = array('success' =>'res',
'level'=>$Level,
'q1'=>$q,
'op1'=>$op1,
'op2'=>$op2,
'ans'=>$ans
);
echo json_encode($response);
?>
我怎样才能获得json字符串(是否可以获得多个字符串)
{'level':'somedata','question':'question1','option1':'option1data','option2':'option2data','answer':'answer1'}
{'level':'somedata','question':'question1','option1':'option1data','option2':'option2data','answer':'answer1'}
答案 0 :(得分:0)
我假设你想要一个对象数组:
$response = array();
$query="Select level, question, option1, option2, answer from quiz where level='$Level'";
$res=mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
$response[] = $row;
}
echo json_encode($response);
是的,你可以拥有任意数量的echo
,但结果输出也应该是JSON。您发布的内容无效JSON。