2017-08-11 32 views
-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中,我总是得到我期待的结果。请帮忙,谢谢!

问候,

克里斯·马蒂诺

+0

** 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

+0

uhhh,whats'$ table_name'? '$ table_name'是否有行,其中'device_number = $ device_number'?你的$ conn是否有效?还有其他的东西? – chiliNUT

+0

我真的希望你不会在不同的表格中存储不同的客户数据。这就是你如何制造不可维护的怪物。 – tadman

回答

0

我想通了。在查询触发之前,我正在关闭与数据库的连接。对不起浪费大家时间!

克里斯

相关问题