在 php 中使用 PDO 类获取 num_rows 时出现问题

发布于 2024-10-30 04:41:06 字数 1026 浏览 1 评论 0原文

我刚刚更改了数据库连接。我还不习惯 PDO 类或 OOP。无论如何,我像这样连接到数据库:

        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
        try
        {
            $this->db = new PDO($dsn, DB_USER, DB_PASS);
        }
        catch ( Exception $e )
        {
            die ( $e->getMessage() );
        }

我试图从此查询中获取行数:

    $ip = $this->ip(); 
    $sql = "SELECT `id` FROM `login_failed`
            WHERE `ip` = :ip AND `time` BETWEEN NOW( ) - INTERVAL 120 MINUTE AND NOW( )
            LIMIT 3";
    try 
    {
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
        $result = $stmt->execute(); // $result = true
        $n = $stmt->num_rows ; // n = NULL?
        $stmt->closeCursor(); 
    }       
    catch (Exception $e)
    {
        die ($e->getMessage() ); 
    }

phpmyadmin 中,我得到一个结果,因此我的查询是正确的,但是 $n 由于某种原因 NULL 。如何使用 PDO 获取行数

I have just changed my database connection. I am not used to the PDO class or OOP yet. Anyway, I connect to the db like this:

        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
        try
        {
            $this->db = new PDO($dsn, DB_USER, DB_PASS);
        }
        catch ( Exception $e )
        {
            die ( $e->getMessage() );
        }

I am trying to get number of rows from this query:

    $ip = $this->ip(); 
    $sql = "SELECT `id` FROM `login_failed`
            WHERE `ip` = :ip AND `time` BETWEEN NOW( ) - INTERVAL 120 MINUTE AND NOW( )
            LIMIT 3";
    try 
    {
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
        $result = $stmt->execute(); // $result = true
        $n = $stmt->num_rows ; // n = NULL?
        $stmt->closeCursor(); 
    }       
    catch (Exception $e)
    {
        die ($e->getMessage() ); 
    }

In phpmyadmin I get a result so my query is correct, but $n is NULL for some reason.. How do I get number of rows with PDO

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

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

发布评论

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

评论(1

薄凉少年不暖心 2024-11-06 04:41:06

$stmt 的类型为 PDOStatement。该类没有 num_rows 属性。

您可能正在寻找 rowCount ,但该文件指出:

如果最后执行的 SQL 语句是
相关的 PDOStatement 是
SELECT 语句,某些数据库可能
返回返回的行数
那个声明。然而,这
不能保证所有人的行为
数据库,不应依赖
适用于便携式应用。

总而言之,如果您想实际SELECT所有数据,您可以通过迭代结果集可靠地确定返回了多少行(或者只调用fetchAll 并计算数组中的项目)。如果您不需要数据而只需要数字,请改用SELECT COUNT

因此,要在不更改查询的情况下计算行数:

$result = $stmt->execute();
$rows = $stmt->fetchAll(); // assuming $result == true
$n = count($rows);

$stmt is of type PDOStatement. That class has no num_rows property.

You might be looking for rowCount instead, but the documentation for that states:

If the last SQL statement executed by
the associated PDOStatement was a
SELECT statement, some databases may
return the number of rows returned by
that statement. However, this
behaviour is not guaranteed for all
databases and should not be relied on
for portable applications.

The long and the short if it is that, if you want to actually SELECT all that data, you can reliably determine how many rows were returned by iterating over the result set (or just call fetchAll and count the items in the array). If you don't need the data but just a number, use SELECT COUNT instead.

So, to count the rows without changing the query:

$result = $stmt->execute();
$rows = $stmt->fetchAll(); // assuming $result == true
$n = count($rows);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文