PDO:fetchColumn是否移动了返回结果集的指针?

发布于 2024-10-15 08:30:01 字数 425 浏览 2 评论 0原文

我最近实现了 PDO,并注意到我的查询结果缺少第一行。这可能是因为 fetchColumn() 检索第一行并将指针移动到第二行,以便 while() 循环从第 2 行开始。这是正确的吗?如果是这样,我该如何避免这种情况并改进以下代码块?

$STH = $DBH->prepare("SELECT * FROM users");
$result = $STH->execute();

if (!$result)
    {
    return false;
    }
elseif($STH->fetchColumn()>0)//counterpart of mysql_num_rows()
    {
    while ($row = $STH->fetch())
        {
        ...
        }
    }
}

I recently implemented PDO and noticed that my query results lacked the first row. That's probably because fetchColumn() retrieves the first row and moves the pointer to the second row so that the while() loop starts at row 2. Is that correct? If so, how can I avoid that and improve the following code block?

$STH = $DBH->prepare("SELECT * FROM users");
$result = $STH->execute();

if (!$result)
    {
    return false;
    }
elseif($STH->fetchColumn()>0)//counterpart of mysql_num_rows()
    {
    while ($row = $STH->fetch())
        {
        ...
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-10-22 08:30:01

正确吗?

是的。此外,fetchColumn() 并不等同于 mysql_num_rows()。相反,fetchColumn() 检索当前行的索引列值(默认为索引 0),并按假设前进游标。

如果您需要计算查询中返回的行数,我建议您首先使用 fetchColumn()< 发出具有相同条件的 SELECT COUNT(1) ... 查询/code> 返回计数。

请参阅本手册页上的示例#2 - http://www.php.net/ Manual/en/pdostatement.rowcount.php


例如

$stmt = $DBH->query('SELECT COUNT(1) FROM users');
// using a straight PDO::query() call as a prepared statement would be
// overkill for these queries

if ($stmt->fetchColumn() == 0) {
    return false;
}

$stmt = $DBH->query('SELECT * FROM users');
while ($row = $stmt->fetch()) {
    ...
}

Is that correct?

Yes. Also, fetchColumn() is not an equivalent for mysql_num_rows(). Instead, fetchColumn() retrieves the indexed column value (defaulting to index 0) of the current row and as assumed, advances the cursor.

If you need a count of the number of rows returned in your query, I suggest you first issue a SELECT COUNT(1) ... query with the same conditions, using fetchColumn() to return the count.

See example #2 on this manual page - http://www.php.net/manual/en/pdostatement.rowcount.php


For example

$stmt = $DBH->query('SELECT COUNT(1) FROM users');
// using a straight PDO::query() call as a prepared statement would be
// overkill for these queries

if ($stmt->fetchColumn() == 0) {
    return false;
}

$stmt = $DBH->query('SELECT * FROM users');
while ($row = $stmt->fetch()) {
    ...
}
梦归所梦 2024-10-22 08:30:01

经过谷歌搜索后,我想出了这个解决方案:

$STH = $DBH->prepare("SELECT * FROM users");

if(!$STH)
    {
    $error = $DBH->errorInfo();
    }
else
    {
    $result = $STH->execute();

    if($result===false)
        {
        return false;
        }
    else
        {
        $rows = $STH->fetchAll(PDO::FETCH_ASSOC);

        if(count($rows) > 0)
            {
            foreach ($rows as $row)
                {
                ...
                }
            }
        }
    }
}

After googling around I came up with this solution:

$STH = $DBH->prepare("SELECT * FROM users");

if(!$STH)
    {
    $error = $DBH->errorInfo();
    }
else
    {
    $result = $STH->execute();

    if($result===false)
        {
        return false;
        }
    else
        {
        $rows = $STH->fetchAll(PDO::FETCH_ASSOC);

        if(count($rows) > 0)
            {
            foreach ($rows as $row)
                {
                ...
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文