我在堆栈上寻找过类似的东西,但没有与此完全相同的东西。
我(认为我)需要在循环内生成唯一的 MySQL 查询,因为每次迭代都需要查找不同的表。该循环来自分解的 $_GET 数组。
问题是基于循环迭代创建一个不同名称的 mysql 查询。我已经在 $var 名称不同的地方完成了它,但它不起作用,我想是因为它是一个字符串而不是变量?
感谢任何帮助
$temps = explode(",", $_GET['temps']);
$tempCount = count($temps);
for ($i=0; $i<$tempCount; $i++)
{
/*'normal' database lookup
$check = mysql_query("SELECT * FROM _db_".$temps[$i]."");
$checks = array();
while ($row = mysql_fetch_assoc($check)) {
$checks[] = $row;
}*/
//here's where I'm trying to build a 'dynamic' lookup for each loop iteration
$checkTemp=$check.$temps[$i];
$checkTempArray=$check.$temps[$i].'Array';
$checkTemp = mysql_query("SELECT * FROM _db_".$temps[$i]."");
$checkTempArray = array();
while ($row = mysql_fetch_assoc($checkTemp)) {
$checkTempArray[] = $row;
}
}
最佳答案
如果我理解正确的话,您正在尝试从 $_GET["temps"]
中由 ,
分隔的所有表中 SELECT *
$temps = explode(",", $_GET['temps']);
$tempCount = count($temps);
$allResults = array();
for ($i=0; $i<$tempCount; $i++)
{
$checkTemp = mysql_query("SELECT * FROM _db_".mysql_real_escape_string($temps[$i]));
$allResults[$temps[$i]] = array();
while ($row = mysql_fetch_assoc($checkTemp))
{
$allResults[$temps[$i]][] = $row;
}
}
// Now for example $allResults["john"][3] contains the fourth row in the table _db_john
print_r($allResults["sally"][2]); // print the third row in _db_sally
关于php - 来自 url 数组的 for 循环中的动态 mysql_query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568954/