如何检查mysql中的当前数字....
我的查询是
$aid = 16;
$get_prev_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id<'$picid' ORDER BY pic_id LIMIT 1");
$get_next_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id>'$picid' ORDER BY pic_id LIMIT 1");
我正在使用以下查询获取当前照片
$photo = mysql_query("SELECT * FROM photos WHERE pic_id='$picid' LIMIT 1");
通过以下查询获取相册中的总照片
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
$total_photos = mysql_num_rows($photos);
现在我想查看我的位置并将其显示为显示20个中的1个,显示20个中的6个等等...
现在我想检查一下我在哪里......
答案 0 :(得分:2)
我认为你指的是分页,可以使用LIMIT和OFFSET sql来实现
决定每页所需的结果数,然后选择那么多
创建如下链接:
<a href="page.php?view=10">View the next 10</a>
并每次动态更改
查询看起来像〜
$offset=$_GET['view'];
SELECT * FROM table WHERE `condition`=true LIMIT 5 OFFSET $offset
这大致翻译为
select 5 from the table, starting at the 10th record
答案 1 :(得分:1)
这很糟糕:
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
因为当你真正想要的只是计数时,它会抓取整张照片专辑的所有字段。相反,获取相册中的照片总数如下:
$result = mysql_query("SELECT count(1) as total_photos FROM photos
WHERE album_id='$aid'");
if ($result === false) {
print mysql_error();
exit(1);
}
$row = mysql_fetch_object($result);
$total_photos = $row->total_photos;
mysql_free_result($result);
现在您拥有相册中照片总数的计数,以便您可以设置分页。比方说,限制设置为每页20张照片。这意味着你可以列出照片1-20,21-40等。创建一个$ page变量(来自用户输入,默认为1),表示你所在的页码,$ limit和$ offset变量插入你的查询。
$limit = 20;
$page = $_POST['page'] || 1; // <-- or however you get user input
$offset = $limit * ($page - 1);
我将离开您编码页面列表的部分。下次根据您创建的变量查询照片。
$result = mysql_query("SELECT * FROM photos WHERE album_id='$aid'
ORDER BY pic_id LIMIT $limit OFFSET $offset");
if ($result === false) {
print mysql_error();
exit(1);
}
$photo_num = $offset;
while ($row = mysql_fetch_object($result)) {
$photo_num++;
$pic_id = $row->pic_id;
// get the rest of the variables and do stuff here
// like print the photo number for example
print "Showing photo $photo_num of $total_photos\n";
}
mysql_free_result($result);
我会留下更好的错误处理,对数据做一些事情,其余细节由你决定。但这是基础知识。此外,我没有检查我的代码是否有错误,因此上面可能存在一些语法问题。要在每页制作一张照片,只需制作$ limit = 1。