如何将查询的第一个结果与其余的分开? [英] How to separate first result of query from the rest?

查看:79
本文介绍了如何将查询的第一个结果与其余的分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PDO结果的第一个数组与其他数组分开.这是我的尝试:

I want to separate first array of PDO-result from other arrays. Here is my try:

$iterator = 1;
while($results = $stmt->fetch(PDO::FETCH_ASSOC) and $iterator == 1){
    /* I need just first row here */
    print_r($results);
    $iterator++;
}

while($results = $stmt->fetch(PDO::FETCH_ASSOC)){
    /* I need other rows here */
    print_r($results);
}

首先在我的代码中执行while()(上面的代码始终只打印查询的第一个结果).好吧,我该如何解决?

But only first while() executes in my code (the above code always just print first result of the query). Well, how can I fix it?

注意:我不想使用fetchAll();.

推荐答案

尝试类似的方法:

$results = $stm1->fetch();
print_r($results);

while($results = $stm1->fetch()){
    print_r($results);
}

您实际上不需要循环来获取第一个元素,然后可以使用以前的第二个循环来恢复迭代.

You don't actually need a loop to get the first element, and then you can resume iteration with what used to be your second loop.

问题

while($results = $stm1->fetch() and $iterator == 1){

是如果结果集中有一个或多个结果,则您将总是 调用$stm1->fetch()两次:第一次进入循环,第二次进入第二次在实际检查($iterator == 1)不再为真之前进行迭代.

is that if there is one or more results in the result set, you will always call $stm1->fetch() twice: once the first time you enter the loop, and then a second time on the second iteration before you actually check that ($iterator == 1) is no longer true.

如果结果集中只有两个结果,则该结果将在第二个循环中显示出来,而不会被输入(因为访存将没有任何结果).

If there are only two results in your result set, then that will manifest in the second loop never being entered (because fetch will not have any results left).

这篇关于如何将查询的第一个结果与其余的分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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