[英]PHP print SQL query in JSON format
I would like to get data from MySQL
database in json
format and then print it to a PHP
page. 我想以
json
格式从MySQL
数据库获取数据,然后将其打印到PHP
页面。
I try to use this script: 我尝试使用此脚本:
<?php
$page = $_GET['page'];
$start = 0;
$limit = 5;
require_once('dbConnect.php');
$total = mysqli_num_rows(mysqli_query($con, "SELECT id from photos"));
$page_limit = $total/$limit;
if($page<=$page_limit){
$start = ($page - 1) * $limit;
$sql = "SELECT * from photos limit $start, $limit";
$result = mysqli_query($con,$sql);
$res = array();
while($row = mysqli_fetch_array($result)){
array_push($res, array(
"location"=>$row['location'],
"image"=>$row['image'])
);
}
echo json_encode($res);
}else{
echo "over";
}
Database connection is OK, but it gives "over" message. 数据库连接可以,但是会显示“ over”消息。 Table contains 1 record, which details should be listed in
JSON
format. 该表包含1条记录,详细信息应以
JSON
格式列出。
$total
is 1. $total
为1。
$limit
is 5. $limit
为5。
$page_limit
is 1/5. $page_limit
为1/5。 (PHP will not cast to int). (PHP不会转换为int)。
$page
is 1. $page
为1。
$page <= $page_limit
is false. $page <= $page_limit
为false。
To make your code work the way you're expecting, make $page_limit = ceil($total/$limit);
为了使您的代码按预期方式工作,请使
$page_limit = ceil($total/$limit);
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.