【发布时间】:2026-02-09 07:30:01
【问题描述】:
如何合并数组以使输出看起来像这样?
($a[0] $b[0] $a[1] $b[1] $a[2] $b[2]...)
问题的背景故事
我正在尝试使用两个数组中的数据(字符串)构建一个字符串到 构建一些 Sql。
这是我从代码中得到的错误集。
数组([0] => test@test.test [1] => testpass [2] =>)
Array ([0] => SELECT hosts.Email, hosts.LName, hosts.FName, hosts.ID FROM hosts WHERE >Email = [1] => AND Password = [2] => ; )
SELECT hosts.Email, hosts.LName, hosts.FName, hosts.ID FROM hosts WHERE Email = AND Password = ;test@test.testtestpassblank line
错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'AND Password = ;test@test.testtestpass' 附近使用正确的语法
我需要帮助的行是
foreach (array_merge($ArrSql, $input) as $SqlArray) {//合并数组构建字符串
我希望输出像
$a[0] $b[0] $a[1] $b[1] $a[2] $b[2]...
但我得到的是
$a[0] $a[1] $a[2] $b[0] $b[1] $b[2]...
好的,这是我的代码碰撞,可能会有所帮助。
感谢您的宝贵时间, 山姆
public function ReturnData($Sql, $BindVars) {
$BindVars[] = '';// needed for pading
$FilledSql = '';
$mysqli = DataBase::ConDataBase();
$ArrSql = explode("?", $Sql); //splitting the code on '?' so that it can dynamically add the values
foreach ($BindVars as $value) {
$input[] = $mysqli->real_escape_string($value);// cleaning the input
}
print_r($input);echo "<br>";//testting to see if data is filled
print_r($ArrSql);echo "<br>". "<br>";//testting to see if array was filled correctly
foreach ( array_merge( $ArrSql, $input) as $SqlArray) {//merge arrays to build string
$FilledSql .= $SqlArray; //anding combind strings from the arrays
}
echo($FilledSql); Echo "blank line" . "<br>". "<br>";//
return $FilledSql;
}
【问题讨论】:
-
我不确定你想要达到什么目的。您是否尝试将每个数组元素打印两次? SQL 错误与此有什么关系?
-
@AmalMurali 我想像这样合并两个数组`$a[0] $b[0] $a[1] $b[1] $a[2] $b[2]。 ..`但我得到的是
$a[0] $a[1] $a[2] $b[0] $b[1] $b[2]... -
您的 SQL 错误是一个SQL injection attack 漏洞。
-
@WhiteShadow:嗯,这正是你在问题中写的。
-
不,这是因为您正在构建不正确的查询。无论您对数组合并问题做什么都不会修复 sql 错误。你有 mysql_real_escape_string() 在那里,但你的
testtest值仍然没有被引用。 m_r_e_s 不会将'-quotes 添加到字符串中。它的唯一功能是在字符串中转义 sql 元字符。