-2
我有幾個數據庫表。他們都充滿了數據。我有以下試圖檢索數據的PHP代碼:PHP mysqli_num_rows始終返回0即使數據庫中的數據也是如此
$sql = "select inspection_date, inspection_type, capacity, criticality, man_hours from $table_name where device_number = '$device_number' order by inspection_date";
//printf("sql = $sql <br/>");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$final_array[] = array("device_number"=>$all_ld_formatted[$i]['device_number'], "old_device_number"=>$all_ld_formatted[$i]['old_device_number'],
"type"=>$all_ld_formatted[$i]['type'], "building"=>$all_ld_formatted[$i]['building'], "room"=>$all_ld_formatted[$i]['room'],
"power"=>$all_ld_formatted[$i]['power'], "inspection_date"=>$row['inspection_date'], "inspection_type"=>$row['inspection_type'],
"capacity"=>$row['capacity'], "criticality"=>$row['criticality'], "man_hours"=>$row['man_hours']);
}
} else {
printf("no results <p/>");
}
上面的代碼運行在for-loop中。所以,這就是爲什麼你在那裏看到$ all_ld_formatted [$ i] ['value']的東西。出於某種原因,代碼總是返回「無結果」。但是,如果我取消註釋printf(「sql = $ sql」);行,並將查詢複製並粘貼到PHPMyAdmin中,我總是得到我期待的結果。請幫忙,謝謝!
問候,
克里斯·馬蒂諾
** WARNING **:當使用'mysqli'你應該使用[參數化查詢(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman
uhhh,whats'$ table_name'? '$ table_name'是否有行,其中'device_number = $ device_number'?你的$ conn是否有效?還有其他的東西? – chiliNUT
我真的希望你不會在不同的表格中存儲不同的客戶數據。這就是你如何製造不可維護的怪物。 – tadman