mysql_fetch_array在while循环中不起作用

时间:2014-12-31 06:00:22

标签: php mysql

我有10个表(table_1,table_2,table_3等),目前我想在循环中获取每个表的结果集,但目前它正在返回错误。

它可以正常工作

$excute = ("CALL Dummy_2('table_1')");
$result = mysql_fetch_assoc(mysql_query($excute));
var_dump($result);

结果

array (size=8)
  'ID' => string '1' (length=3)
  'name' => string 'Test_E' (length=11)
  'accountname' => string 'sri01' (length=3)
  'accountID' => string '1' (length=1)
  'status' => string '2' (length=1)
  'total_mps' => string '202' (length=3)
  'min(a.timestamp)' => string '2014-05-16 05:38:01' (length=19)
  'max(a.timestamp)' => string '2014-12-31 03:41:31' (length=19)

但是当我把它放在循环中以满足我的要求时,它会返回9个错误(等于剩余的表数)以及第一个结果集

$table_count = mysql_query("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'milepostdb' AND table_name LIKE 'table_%' ");

while($row = mysql_fetch_array($table_count)){
$table = $row["TABLE_NAME"];

$excute = ("CALL Dummy_2('{$table}')");
$result = mysql_fetch_assoc(mysql_query($excute));
var_dump($result);
} 

错误

 array (size=8)
      'ID' => string '1' (length=3)
      'name' => string 'Test_E' (length=11)
      'accountname' => string 'sri01' (length=3)
      'accountID' => string '1' (length=1)
      'status' => string '2' (length=1)
      'total_mps' => string '202' (length=3)
      'min(a.timestamp)' => string '2014-05-16 05:38:01' (length=19)
      'max(a.timestamp)' => string '2014-12-31 03:41:31' (length=19)

( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean .......

null

( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean .......

null

( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean .......

null

etc

1 个答案:

答案 0 :(得分:0)

函数mysql_fetch_assoc(mysql_query($ excute))在查询未成功执行时返回NULL。

尝试按如下方式替换$ execute。

$ excute =“CALL Dummy_2('”。$ table。“')”;

在您的情况下,表名称将被搜索为{$ table},并且查询中不会替换该变量。此结果是错误的搜索,不会找到任何表格。

相关问题