首页 > 解决方案 > 执行mysqli语句后如何获取行数?

问题描述

我想将返回语句的行数存储在会话变量中,但我不知道如何。我正在尝试这样做,但它不起作用。

$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();

标签: phpsessionmysqlirow

解决方案


我已经解决了这个问题,它的工作原理是这样的:

$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'];

推荐阅读