使用php在url中发送json数据时无法检索数据库值

时间:2013-10-05 05:48:41

标签: php json

我的php中有以下问题。

我将json对象作为url中的参数发送到我的php文件。但是在这里我无法检索数据库值。,输出只是null ..

我的网址:test1.php?alb_id = {“album_ids”:[{“alb_id”:“2”},{“alb_id”:“4”}}}

我的php文件:

 $album_ids = $_REQUEST['alb_id'];

 $id_list_array = json_decode($album_ids);

 $id_array = $id_list_array->album_ids;

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

我怎样才能得到正确的结果?

1 个答案:

答案 0 :(得分:1)

您需要对网址中的特殊字符进行百分比编码。将其更改为:

test1.php?alb_id=%7B%22album_ids%22%3A%5B%7B%22alb_id%22%3A%222%22%7D%2C%7B%22alb_id%22%3A%224%22%7D%5D%7D

我通过简单地运行PHP脚本得到了这个:

<?php
echo urlencode('{"album_ids":[{"alb_id":"2"},{"alb_id":"4"}]}');