动态编码数据json与限制使用PHP移动应用程序,如android, ios


dynamically encode data in json with limit using php for mobile app like android , ios

我有1000条以上的记录。我只提供50条记录,每个请求,如0-50, 50-100, 100-150。我使用下面的代码:

    public function get_database($data)
    <?php
    {     
         $start          = $data['start'];
         $limit          = $data['limit']; 
         $alumni_details = array();

   $query1    = "select * from alumni where
   status='Active',limit '".$start."','".$limit."' ";
         $query_run = mysql_query($query1);
         while($row = mysql_fetch_assoc($query_run))
         { 
              $row['date_of_birth'] = date('d M, Y',  strtotime($row['date_of_birth']));  
              $alumni_detail['alumni_details'] = $row;
              $alumni_details[]                = $alumni_detail;
         }
         echo json_encode($alumni_details);   
    }

但我只需要基于user_id,我需要在json中动态编码数据与限制。

您的代码如下:

$start = $data['start'];
$query = SELECT * FROM `alumni` WHERE `status` = 'Active' ORDER BY `alumni_id` LIMIT $start,5"

它应该是动态的5不会被当作常数。取一个变量,是的,它应该是固定的,但根据将来的要求,它可以改变,所以把它放在变量中。

$start=$data['start'];
$query=select * from alumni where status='Active' order by alumni_id Limit $start,5"

其中5为极限,根据要求设置

$start=$data['start'];

$query=select * from alumni where status='Active' order by alumni_id

您的查询字符串有语法错误。

使用串联,例如:

$query1 = "select * from alumni where status='Active' limit ".$start.",".$limit;

或者直接将变量放入双引号字符串中:

$query1 = "select * from alumni where status='Active' limit $start, $limit";

还有一个错别字:您应该在查询中使用$start变量,而不是$star