怎么形式数组在做什么?

时间:2013-09-09 22:05:24

标签: php arrays json

我尝试在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":""}]"

1 个答案:

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

替换{} of while之间的内容

希望这会有所帮助。