php 在PHP中使用SQlite3如何计算结果集中的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2586598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:01:53  来源:igfitidea点击:

Using SQlite3 in PHP how to count the number of rows in a result set?

phpcountsqlite

提问by Remover

currently I am using:

目前我正在使用:

$result = new SQLite3(sprintf("users/USERIDS_DB.sqlite"));

$numRows = $result->exec ("SELECT count(*) FROM USERIDS");

echo sprintf("the number of rows are: %d", $numRows);

but the result is 1 when it should be 6 (the number of rows I created using firefox sqlite3 addon)

但结果是 1,而它应该是 6(我使用 firefox sqlite3 插件创建的行数)

Can anybody help please?

有人可以帮忙吗?

回答by RoboTamer

$db = new SQLite3('filename.db3');
$count = $db->querySingle("SELECT COUNT(*) as count FROM tablename");
echo $count;

回答by Felix Kling

From the documentation:

文档

public bool SQLite3::exec ( string $query )

Executes a result-lessquery against a given database.

public bool SQLite3::exec ( string $query )

对给定的数据库执行无结果查询。

This methods returns a boolean, not a result set. When you convert trueto an integer it will become 1.

此方法返回一个布尔值,而不是结果集。当您转换true为整数时,它将变为1.

You should use SQLite3::query(). Example (untested):

你应该使用SQLite3::query(). 示例(未经测试):

$rows = $result->query("SELECT COUNT(*) as count FROM USERIDS");
$row = $rows->fetchArray();
$numRows = $row['count'];


Btw, naming the instance of the SQLite3 class $resultcan be misleading (especially in a DB environment). I would call it $dbor $connection.

顺便说一句,命名 SQLite3 类的实例$result可能会产生误导(尤其是在数据库环境中)。我会称它为$db$connection

回答by fordiy

$result = $db->query("SELECT * FROM db_name")
$row=$result->fetchArray(SQLITE3_ASSOC);
    // check for empty result
    if ($row != false) {
      // do something here if record exists
    }

回答by Simbarashe D Andrea

<?php
    function SqliteNumRows($query){
        $numRows = 0;
        while($rows = $query->fetchArray()){
            ++$numRows;
        }
        return $numRows;
    }
?>

This really helped me it works actually you may try it

这真的对我有帮助,实际上你可以试试

回答by russell

Here is a working way to get the number of rows since neither sqlite_num_rows($result)nor $result->numRows()works on SQLite3:

这里是工作的方式来获得,因为没有行数sqlite_num_rows($result),也不$result->numRows()适用于SQLite3的:

<?php
     $db = new SQLite3('database.db');

    $results = $db->query('SELECT COUNT(*) FROM (SELECT `id`,* FROM `table` ORDER BY `id` ASC);');
        while ($row = $results->fetchArray()) {
           echo $row["COUNT(*)"];
        }
?>

回答by Nabi K.A.Z.

It's have benefit, when you have conditions in the count selectquery:

当您在计数select查询中有条件时,它有好处:

$stmt = $db->prepare('SELECT COUNT(*) AS `count` FROM `tablename` WHERE `foo`=:foo');
$stmt->bindValue(':foo', $foo);
$stmt->execute();
$count = $stmt->fetchColumn();
echo $count;