douci1677
2014-09-10 06:13
浏览 51
已采纳

php的json输出中的未定义变量

I just don't get what's wrong in my code!

In browser, it shows that undefined variable!

But I declared it in a while loop before!

In browser it shows: Notice: Undefined variable: dhkBlood in C:\xampp\htdocs\JSONdata.php on line 371*

The PHP Code that I wrote is something like below:

if($retrieve1){
    while($row = mysql_fetch_assoc($retrieve1))
    {   
        $dhkBlood[] = array("ID" => $row['PID'], "PlaceName" => $row['PName'], "Address" => $row['Address'], "DeploymentName"  => $row['DName'], "Latitude" => $row['Latitude'], "Longitude" => $row['Longitude']);
    }
}

Code:

370. $result = array();
371. $result["dhakaBlood"]=$dhkBlood;
372. $finalResult = array();
373. $finalResult['data']=$result;
374. echo json_encode($finalResult);

P.S. $retrieve1 variable here is a variable that I used to assign a mysql query that generally retrieves information from my database!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyimiao1993 2014-09-10 06:19
    已采纳

    Add

       $dhkBlood = array();
    

    Before

     if($retrieve1){
      while($row = mysql_fetch_assoc($retrieve1))
       {   
        $dhkBlood[] = array("ID" => $row['PID'], "PlaceName" => $row['PName'], "Address" => $row['Address'], "DeploymentName"  => $row['DName'], "Latitude" => $row['Latitude'], "Longitude" => $row['Longitude']);
       }
      }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doupinyan0186 2014-09-10 06:19
    $dhkBlood = array(); //defined array first 
    if($retrieve1){
    
        while($row = mysql_fetch_assoc($retrieve1)) {
    
            $dhkBlood[ID] = $row['PID'];
            $dhkBlood[PlaceName] = $row['PName'];
            $dhkBlood[Address] = $row['Address'];
            $dhkBlood[DeploymentName] = $row['DName'];
            $dhkBlood[Latitude] = $row['Latitude'];
            $dhkBlood[Longitude] = $row['Longitude'];
    
        }
    
    }
    

    defined array before use, and assign value in array as above,

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题