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
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
'.$row['name'].'
'; } } else { echo "Sold out"; } that returns sold out only no matter what I change query to – Ausghostdog Sep 16 '19 at 12:16