我尝试在do-while中形成数组:
$result = mysql_query("SELECT * FROM data WHERE company='$companyID'", $db);
if (mysql_num_rows($result) > 0)
{
$resultData = mysql_fetch_array($result);
do
{
$json[] = array('product' => $resultData['product'], 'title' => $resultData['title']);
}
while($resultData = mysql_fetch_array($result));
echo json_encode($json);
}
退出所有数据时为空(错误):
"[{"product":"","title":""},{"product":"","title":""},{"product":"","title":""},{"product":"","title":""}]"
答案 0 :(得分:0)
它确实给你一个错误吗?
尝试打印输出,看看你是否实际从mysql中获取任何内容
$result = mysql_query("SELECT * FROM data WHERE company='".$companyID."'", $db);
if (mysql_num_rows($result) > 0)
{
while($resultData = mysql_fetch_array($result))
{
echo '<pre>';
print_r($resultData);
echo '</pre>';
}
}
如果您确实以这种方式获得输出,只需用echo json_encode($json);
希望这会有所帮助。