This article mainly introduces the method of obtaining MySQL data in PHP infinite loop. Interested friends can refer to it. I hope it will be helpful to everyone.
The details are as follows:
public function get_data($limit){ $sql="select * from ((select id,name from `mytable` limit {$limit},10) union all (select id,name from `mytable` limit 0,10)) as test limit 0,10"; return $this->query($sql); }
The above sql statement uses the union all method of mysql to splice the two sets together and take the first ten data.
public function getCount(){//获取数据的条数 $sql="select count(id) as t from `mytable`"; return $this->query($sql); }
The next step is to obtain the data in the controller and provide a data interface to ajax.
//测试数据库无限循环取数据 public function getInfiniteData(){ //用户点击数 $page = $_GET['click']; //每次展示条数 $pagesize = 10; //获取总条数 $total = $this->Mydemo->get_count(); $t = $total[0][0]['t']; //算出每次点击的其起始位置 $limit = (($page - 1)*$pagesize)%$t; $data = $this->Mydemo->get_data($limit); if (!empty($data)) { //转换为二维数组 $list = []; foreach ($data as $key => $v) { $list[$key] = $data[$key][0]; } $info['msg'] = $list; $info['code'] = '001'; }else{ $info['code'] = '002'; $info['msg'] = '暂无数据'; } echo json_encode($info,JSON_UNESCAPED_UNICODE);die; }
Related recommendations:
Use CSS3 to achieve infinite loop seamless scrolling effect
Infinite loop code sharing in VUE
EasyUI Tree tree component infinite loop instance analysis
The above is the detailed content of PHP infinite loop to get MySQL data. For more information, please follow other related articles on the PHP Chinese website!