php mysqli stmt count num_rows


php mysqli stmt count num_rows

我需要获得结果行计数

$this-> db = new mysqli("localhost", "***", "***", "***") or die("Error ".mysqli_error($link));
$this-> db -> set_charset("utf8");
$query = "SELECT steps.id, steps.description, steps.sort, services.`name` AS service_name, services.short_name AS service_short_name, terminals.`name` AS terminal_name, terminals.short_name AS terminal_short_name FROM steps INNER JOIN instructions ON steps.page_id = instructions.id INNER JOIN services ON instructions.service_id = services.id INNER JOIN terminals ON instructions.terminal_id = terminals.id WHERE services.short_name = '{$service}' AND terminals.`name` = '{$terminal}' ORDER BY steps.sort";
if ($stmt = $this->db-> prepare($query)) {
    $stmt -> execute();
    $stmt -> store_result();
    printf("row count: %d.'n", $stmt -> num_rows);
    $stmt -> close();
}

它返回零-0,但有一些类似10-15行的

尽管"我做什么不正确"对这个网站来说是一个离题的问题,但答案是:

说到获取行数,您所做的一切都是正确的
麻烦的唯一原因是一些数据/代码不一致,你必须自己解决。

说到使用已准备好的语句,您使用它们是错误的,将变量直接添加到查询中,而您应该用占位符表示它们并稍后绑定。