0

I want to store in a session variable the number of rows that returns a statement, but I don´t know how. I´m trying doing it like this, but it doesn´t work.

$stmt = $con->prepare("SELECT model, type, color FROM item WHERE model LIKE '%$type%'");


$stmt->execute();


$_SESSION['numRows'] = $stmt->num_rows;
echo $_SESSION['numRows'];

$result = $stmt->get_result();

$_SESSION['itemsInfo'] = $result->fetch_all();
  • why not just count your fetched data array? Also, what lib are you using? PDO? Mysqli? And you're missing a quote from a statement - not sure if that's like that in the code or just on here^^ – treyBake Nov 15 '19 at 10:55
  • I´m using Mysqli, and yes, the quote missing is only here. By the way, thank you, with count I think that works well – Andoni Díez Guerra Nov 15 '19 at 11:04
  • awesome :D I think there is a dupe for this, so may be closed, but you'll find a good answer there :) – treyBake Nov 15 '19 at 11:06
  • See [How to get count of rows in MySQL table using PHP?](https://stackoverflow.com/q/58227521/1839439) – Dharman Nov 15 '19 at 21:49

1 Answers1

-1

I have solve the problem, it works like this:

$stmt = $con->prepare("SELECT model, type, color FROM item WHERE model LIKE '%$type%'");


$stmt->execute();

$result = $stmt->get_result();

$_SESSION['itemsInfo'] = $result->fetch_all();
$_SESSION['numRows'] = count($_SESSION['itemsInfo']);
echo $_SESSION['numRows'];