mysql_fetch_array 在 while 循环中不起作用 [英] mysql_fetch_array does not work inside the while loop
问题描述
我有 10 个表,称为(table_1、table_2、table_3 等),目前我想在循环中获取每个表的结果集,但目前它正在返回错误.
I have 10 tables known as(table_1,table_2,table_3 etc), currently i want get the result set of each of those tables inside a loop but currently it is returning an error.
它像这样工作得很好
$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 个错误(等于剩余的表数)以及第一个结果集
but when i put it inside a loop to fulfill my requirement it returns 9 errors(equal to the remaining number of tables) along with the first result set
$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.
The function mysql_fetch_assoc(mysql_query($excute)) returns NULL when the query is not executed successfully.
尝试替换 $execute 如下.
Try replacing $execute as follows.
$excute = "CALL Dummy_2('".$table."')";
$excute = "CALL Dummy_2('".$table."')";
在您的情况下,表名被搜索为 {$table} 并且该变量不会在查询中被替换.此结果是错误的搜索,将找不到任何表格.
In your case the table name is searched as {$table} and the variable is not replaced in the query. This results is a wrong search and no tables will be found.
这篇关于mysql_fetch_array 在 while 循环中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!