如何编写PHP代码一次一次通过记录? [英] How to write PHP code to go through records one at a time?

查看:81
本文介绍了如何编写PHP代码一次一次通过记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是php/mysql的新手.我正在尝试使用一个非常小的数据库(只有10条记录的单个表)进行测试.

So I'm new to php/mysql. I am testing with a very small database (single table with only 10 records) as I'm just trying to learn.

我正在php页面上显示以下结果:从mytable中选择*,其中city ="atlanta"

I'm displaying the following result on a php page: select * from mytable where city = "atlanta"

它有效..结果显示在我的页面上.现在,我想知道如何在此记录下添加一个链接,显示显示下一个结果",然后单击该链接,就可以在数据库中简单地弹出一个包含下一个结果的新页面?

It works.. the result shows up on my page. Now, I'm wondering, how can I put a link under this record that says "show next result" and on clicking it, it simply pulls up a new page with the next result in my database?

这是否需要完整的分页脚本,或者有简单的方法吗?

Does this require a whole pagination script or is there a simple way?

谢谢!

推荐答案

最简单的方法是包括一个整数$_GET变量,该变量是行号+ 1.

The easiest way would be to include an integer $_GET variable that is the row number + 1.

<a href="index.php?id=<?php echo ($id + 1) ?>" />

然后对于您的PHP脚本,您需要获取该值,或者,如果未提供该值,则使用默认值(0).

Then for your PHP script, you need to get the value, or use a default value (0) if one is not provided.

<?php
    if(!isset($_GET['id']))
        $id = 0;
    else
        $id = $_GET['id'];
?>

别忘了绑定整数,这样就不会得到无效的结果.在您的情况下,请确保$id不会低于0或高于9.简单的东西

Don't forget to bound your integer, so you can't get invalid results. In your case, make sure that $id does not go below 0 or above 9. Something simple like

    if($id < 0) $id = 0;

    if($id > 9) $id = 9;

应该工作.这会在上一个代码示例之后立即进行.

should work. This goes immediately after the previous code sample.

然后针对您的查询,只需

Then for your query, simply do

$sql = "select * from mytable where city = 'atlanta' LIMIT {$id}, 1";

请注意此处的LIMIT子句带有两个参数.首先,您当前的$id编号告诉查询从哪里开始查找结果集中的内容.第二个参数告诉它应该从一开始就获得多少结果.在这种情况下,我们总是想要一个,因此将其保留为1.

Notice the LIMIT clause here that takes two parameters. The first, your current $id number tell the query where to start looking in the result set. The second parameter tells it how many results from the start it should take. In this case, we always want one, so we'll leave it as 1.

当然,您应该对这个查询进行参数化设置,但是由于听起来您只是在学习SQL,所以这没什么大不了的.如果这是一个公共站点,则需要这样做.

Of course, you should parametrize this query, but since it sounds like you're just learning SQL, that's not a huge deal. If this was a public site, you would need to though.

您还可以使用mysql_num_rows或等效的PDO来获取适用行的总数,然后确定$id + 1是否大于总行数(意味着您在最后一条记录中),如果是, ,不显示指向下一条记录的链接.

You could also use mysql_num_rows or the PDO equivalent to get the total number of applicable rows, then determine whether $id + 1 is greater than the total number of rows (meaning you were on the last record), and if so, not display the link to the next record.

完整代码

<?php
    if(!isset($_GET['id']))
        $id = 0;
    else
        $id = $_GET['id'];

    if($id < 0) $id = 0;

    if($id > 9) $id = 9;

    $sql = "select * from mytable where city = 'atlanta' LIMIT {$id}, 1";

    $result = mysql_fetch_assoc($sql);
?>

Display your result here

<?php if( ( $id + 1 ) < mysql_num_rows($result) ) :

<a href="index.php?id=<?php echo ($id + 1) ?>" />

<?php endif; ?>

这篇关于如何编写PHP代码一次一次通过记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆