简体   繁体   English

如何从数据库表中获取下一行?

[英]How to get the next row from the Database table?

How to get the next row from the Database table? 如何从数据库表中获取下一行? Such that the row might be incremented row that is if structure has a field called "id" then the row can be id++ or very next incremented row, but it may also b any other id containing row (NOT VERY NEXT) , (because the ids could be deleted from the table). 这样,该行可能是递增行,也就是说,如果结构中有一个名为“ id”的字段,则该行可以是id ++或紧邻的下一个递增行,但它也可以是任何其他包含id的行(NOT VERY NEXT),(因为ID可以从表格中删除)。

I am using Mysql Database below is my code.. 我正在使用Mysql数据库,下面是我的代码。

mysql_select_db('pranav_test');
$seletaudit = mysql_query("SELECT * FROM jos_audittrail WHERE live = 0");
while($row2 = mysql_fetch_array($seletaudit))
{
    $audit[] =$row2;
}
$trackid = array();
$tracktable = array();
$i = 0;
foreach($audit as $val)
{
    $trackid[$i] = $val['trackid'];
    $tracktable[$i] = $val['table_name'];   

    if($i!=0)
    {
        $prev_id = $trackid[$i-1];
        $prev_valtab = $tracktable[$i-1];
    }
    if($val['operation'] == 'INSERT')
    {
        if($tracktable[$i]!=$prev_valtab)
        {
            $inserttable = "INSERT INTO '".$tracktable[$i]."' (";
        }


        if($tracktable[$i]==$prev_valtab)
        {
            $insertfield .= "'".$val['field']."', ";
        }
        if($tracktable[$i]==$prev_valtab)
        {
            $insertfield .= "]";
            $insertfield = str_replace(", ]",")",$insertfield);

        }
    }
}

here above I need t know whats going to be my next rows's "table_name" field contain... how can i do that....? 在上面我不需要知道下一行的“ table_name”字段将包含什么内容...我该怎么做...? Please help for fetching the next row in the for loop to get it checked. 请帮助获取for循环中的下一行以进行检查。

UPDATE : 更新

Ok, now that you posted some code, here a more specific answer: 好的,既然您已经发布了一些代码,这里是一个更具体的答案:

If you want to have access to the next row in the array, I suggest to use a normal for loop to have access to the index: 如果要访问数组中的下一行,建议使用普通的for循环访问索引:

for($i = 0; $i<count($audit); $i++) {
     // $audit[$i] is current row
     // $audit[$i+1] is next row
}

But be careful when you reach the end of the array, as $i+1 will be out of bounds. 但是,当您到达数组末尾时要小心,因为$i+1会超出范围。

Or if you just wonder why the $i in your current code is not behaving correctly: You have to increment it at the end of your foreach loop: 或者,如果您只是想知道为什么当前代码中的$i行为不正确:您必须在foreach循环的末尾将其递增:

$i++;

UPDATE2 : UPDATE2
A more complete example to make it clear (I adapt your approach and check for the previous row) 一个更完整的示例可以清楚地说明(我采用了您的方法,并检查了一行)

for($i=0; $i<count($audit);$i++)
{
    $curr = $audit[$i];

    if($i>0) {
        $prev = $audit[$i-1];

        if($curr['operation'] == 'INSERT')
        {
            if($curr['table_name']!=$prev['table_name'])
            {
                $inserttable = "INSERT INTO '".$curr['table_name]."' (";
            }
            else {
                // whereever $insertfield comes from
                $insertfield .= "'".$curr['field']."', ";
                $insertfield .= "]";
                $insertfield = str_replace(", ]",")",$insertfield);

            }
        }
    }
}

As you have not specified at all how you connect to the database, here is a generic answer: 由于您根本没有指定连接数据库的方式,因此有一个通用的答案:

Specify your query to order the result set by ID: 指定查询以按ID排序结果集:

$query = "SELECT <columns here> FROM <table here> ORDER BY id";

Then get the result set (we have a fictive database class): 然后获取结果集(我们有一个虚拟的数据库类):

$result = $db->query($query);

Then iterate over the resultset: 然后遍历结果集:

while($row = $result->next()) {
    // do something with row
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM