我想使用json_decode來結合幾個json對象..我需要檢索基於專輯id的數據庫值。但我不能。如何做json_encode的json_decode
誰能告訴我我犯了什麼錯誤嗎?
$album_ids_id = array("album_ids"=>array(2,4,5));
// $album_ids = $_REQUEST['alb_id'];
$album_ids = json_encode($album_ids_id);
$id_list_array = json_decode($album_ids,true);
$id_array = $id_list_array->album_ids;
var_dump($id_list_array);
for($i=0;$i<sizeof($id_array);$i++)
{
$alb_id = $id_array[$i]->alb_id;
$album_sel_query = "SELECT a.a_id as id,a.a_name as name,round((b.total_value/b.total_votes),1) as rating,b.total_votes,b.total_value,a.a_pic as image,c.b_name FROM _album a inner join ratings b on b.id=a.a_id INNER JOIN _band c on c.b_id=a.b_id where a.a_id='".$alb_id."' ";
$result = mysql_query($album_sel_query);
if (!$result)
die("mySQL error: ". mysql_error());
$count = mysql_num_rows($result);
if($count > 0)
{
while($data = mysql_fetch_array($result))
{
$alb_name =$data['name'];
$singer = $data['b_name'];
$rating = $data['rating'];
$rate_value = $data['total_value'];
$rate_votes = $data['total_votes'];
$alb_pic =$data['image'];
$resmsg[] = array("Album_id"=>$alb_id,"Album_name"=>$alb_name,"Album_singer"=>$singer,"Album_rating"=>$rating,"Rating_total_value"=>$rate_value,"Rating_total_votes"=>$rate_votes,"Album_image_name"=>$alb_pic);
}
$jsonarr = array("response"=>array("success"=>"Y","ALBUM_DETAILS"=>$resmsg));
}
else
{
$jsonarr = array("response"=>array("success"=>"N","ALBUM_DETAILS"=>"Data not found"));
}
}
echo json_encode($jsonarr);
因爲我曾經使用過var_dump(),.輸出顯示
array(1) { [0]=> array(1) { ["album_ids"]=> array(1) { ["alb_id"]=> string(1) "5" } } } {"response":{"success":"N","ALBUM_DETAILS":"Data not found"}}
謝謝你。我有適當的數組格式。但仍然在檢索數據庫值 –