PHP foreach,数组查询中使用的数据
|
我有:
$array1 = //contains places ids and locations;
$array2 = array();
$array3 = array();
foreach($array1 as $itemz)
{
$array2[] = $itemz[place][id];
$array3[] = $itemz[place][location][city];
$sql = \"select * from places where id=\".$array2.\" and location=\".$array3.\"\";
}
但是当我打印$ sql时,我得到:
select * from places where id=12 and location=Array
谁能告诉我代码有什么问题吗?
谢谢!
没有找到相关结果
已邀请:
4 个回复
蕉衫
舜辉
您在这里缺少引号,请添加它们
数组到字符串的转换
该语句不起作用有两个原因。 假设
是INT的单个字段,并且在
中有很多INT,那么如果没有MySQL
,仍然无法比较它们。 您正在从PHP数组转换为字符串。那行不通。 由于您是在循环中运行
和ѭ10continue,因此它们将继续变化并不断增长。 因此,您实际上要尝试的是提出一个类似的查询
但这根本没有任何意义,因为随着循环的继续,您将以增量方式检索相同的数据。 所以我认为您真正想做的是
这很可能是您所需要的。当您遍历数组时,这将检索每一行的行。 我会做一些改进。 循环完成后,运行一次查询,因此您只需运行一次查询,而不必运行“ 13”次。 另外,考虑只检索所需的列,而不要执行
蹄渭信妥扳
寒健
结果将创建一个名为
的数组,并向其添加键为
的等于
的元素。当您尝试将此变量嵌入查询中时,会遇到问题,因为它不是字符串。 您可能需要的是:
请注意,我已经进行了更改,以解决代码中的其他一些严重问题(用常量而不是字符串索引到数组中,并使代码容易受到SQL注入的攻击)。