php mysqli stmt count num_rows

时间:2013-08-31 09:32:25

标签: php mysql mysqli

我需要获得结果行计数

$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行

1 个答案:

答案 0 :(得分:1)

虽然"我做错了什么"是这个网站的一个offtopic问题,答案是:

说到获得行数,你所做的一切都是正确的 您遇到麻烦的唯一原因是您需要自行解决的一些数据/代码不一致问题。

说到使用预处理语句,你使用它们是错误的,直接在查询中添加变量,而你应该用占位符表示它们并稍后绑定。