2013-08-31 147 views
-2

我需要得到的結果行數PHP mysqli的語句數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(); 
} 

它返回零 - 但也有一些像10-15行

+0

你在你的數據庫中至少有一行/ –

+0

@DarylGill我有一些像10-15行 – user2368299

+1

你測試了你的查詢在phpmyadmin? – Mihai

回答

1

雖然「我做的不正確」是一個這個網站的外觀問題,答案是:

說到獲取行數,你做的一切都是正確的。
你的麻煩的唯一原因是你必須解決自己的一些數據/代碼不一致。

說到使用準備好的語句,您正在使用它們是錯誤的,將變量直接添加到查詢中,而您應該用佔位符表示它們並稍後進行綁定。