我有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

最佳答案

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

尝试如下替换$ execute。

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

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

关于php - mysql_fetch_array在while循环中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27716838/

10-15 00:59