php - 来自 url 数组的 for 循环中的动态 mysql_query

标签 php mysql for-loop

我在堆栈上寻找过类似的东西,但没有与此完全相同的东西。

我(认为我)需要在循环内生成唯一的 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/

相关文章:

mysql - 在mysql中同时替换一些条目

java - 在数组中添加元素

javascript - 最多显示 10 种可用产品

generics - 如何推断 for-generate block 中的变量名称?

php - 在 symfony 中哪里存储用户可编辑的配置参数?

php - 组织php项目文件

mysql - 在 MySQL 中使用哪种文本数据类型是正确的?

java - MySQL - 如何使用正确的时区存储时间? (来自 java )

php - 你可以在 MySQL 中使用 "On Duplicate Key Update"的准备好的语句吗?

php - 什么会导致我的脚本在 magento 中的 css 之前加载