简体   繁体   English

SQLite3计数行

[英]SQLite3 count rows

I am trying to count the number of rows returned from a query 我正在尝试计算查询返回的行数

This is currently the code I have 这是我现在的代码

function products()
{
$loggedin = loggedin();
$db = db();

$stmt = $db->prepare('SELECT game_id, name, developer, price FROM game   WHERE quantity > 0 ORDER BY game_id DESC');
$result = $stmt->execute();
$row1 = $result->fetchArray();
$rows = count ($row1); 

if ($rows == 0)
    {
    echo "Sold out";
    }
else
    {
    while ($row = $result->fetchArray()) {
    echo '<p>'.$row['name'].'</p>';

    }
    }
}

At present it will only return the first item in the table, even though all rows match 目前,即使所有行都匹配,它也只会返回表中的第一项

if I change my code to 如果我将代码更改为

function products()
{
$loggedin = loggedin();
#$page = 'index.php';
$db = db();
$stmt = $db->prepare('SELECT game_id, name, developer, price FROM game   WHERE quantity > 11 ORDER BY game_id DESC');
$result = $stmt->execute();
while ($row = $result->fetchArray()) {
    echo '<p>'.$row['name'].'</p>';

}

Then I get all the result from the table correctly 然后我从表中正确获得所有结果

if I change the query to a value outside the quantity like 如果我将查询更改为超出数量的值,例如

quantity > 20

then I get the error 然后我得到了错误

Warning: count(): Parameter must be an array or an object that implements Countable

what I would like it to do is if row1 is == 0 the show sold out other wise display all the rows 我想做的是,如果row1 == 0,则该节目售罄,否则将显示所有行

so if a row does not match the query it will not be included in the results I can not use PDO, and I have not been able to find out to use count on https://www.php.net/manual/en/book.sqlite3.php 因此,如果某行与查询不匹配,则该行将不包含在结果中,我无法使用PDO,并且无法在https://www.php.net/manual/en/上找到要使用的计数book.sqlite3.php

That should do it. 那应该做。

function products() {
    $loggedin = loggedin();
    $db = db();
    $games = $db->query('SELECT game_id, name, developer, price FROM game WHERE quantity > 0 ORDER BY game_id DESC');

    if (empty($games->fetchArray())) {
        echo "Sold out";
        return;
    }

    // Reset the result back to the first game
    $games->reset();

    while ($game = $games->fetchArray(SQLITE3_ASSOC)) {
        echo '<p>'.$game['name'].'</p>';  
    }
}

I'd do something like: 我会做类似的事情:

function products() {
    $db = db();
    $stmt = $db->prepare('SELECT game_id, name, developer, price FROM game WHERE quantity > 0 ORDER BY game_id DESC');
    $result = $stmt->execute();
    $count = 0;

    while ($row = $result->fetchArray()) {
        echo '<p>'.$row['name'].'</p>';
        $count++;  
    }

    if ($count == 0) {
        echo "<p>Sold out</p>";
    }
}

The only way to find out how many rows were returned by a query is to fetch all of them (and you don't actually care how many rows were returned, just that at least one was). 找出查询返回多少行的唯一方法是获取所有这些行(您实际上并不关心返回的行数,只是至少返回了一行)。 So just increment a counter once per row returned, and after stepping through all rows, if that counter is still 0, it means no rows were returned, and you can display the relevant message. 因此,只需为返回的每一行增加一个计数器,然后逐步浏览所有行,如果该计数器仍为0,则表示没有返回任何行,您可以显示相关的消息。

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

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