PHP 内爆关联数组
所以我试图创建一个函数来生成基于多维数组的 SQL 查询字符串。
示例:
function createQueryString($arrayToSelect, $table, $conditionalArray) {
$queryStr = "SELECT ".implode(", ", $arrayToSelect)." FROM ".$table." WHERE ";
$queryStr = $queryStr.implode(" AND ",$conditionalArray); /*NEED HELP HERE*/
return $queryStr;
}
$columnsToSelect = array('ID','username');
$table = 'table';
$conditions = array('lastname'=>'doe','zipcode'=>'12345');
echo createQueryString($columnsToSelect, $table, $conditions); /*will result in incorrect SQL syntax*/
如您所见,我需要有关第三行的帮助,因为它当前正在打印
从表 WHERE 中选择 ID、用户名 姓氏和邮政编码
,但应该打印
从表 WHERE 中选择 ID、用户名 姓氏 = 'doe' AND 邮政编码 = '12345'
So I'm trying to create a function that generates a SQL query string based on a multi dimensional array.
Example:
function createQueryString($arrayToSelect, $table, $conditionalArray) {
$queryStr = "SELECT ".implode(", ", $arrayToSelect)." FROM ".$table." WHERE ";
$queryStr = $queryStr.implode(" AND ",$conditionalArray); /*NEED HELP HERE*/
return $queryStr;
}
$columnsToSelect = array('ID','username');
$table = 'table';
$conditions = array('lastname'=>'doe','zipcode'=>'12345');
echo createQueryString($columnsToSelect, $table, $conditions); /*will result in incorrect SQL syntax*/
as you can see I need help with the 3rd line as it's currently printing
SELECT ID, username FROM table WHERE
lastname AND zipcode
but it should be printing
SELECT ID, username FROM table WHERE
lastname = 'doe' AND zipcode = '12345'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您实际上并没有内爆多维数组。 $conditions 是一个关联数组。
只需在函数 createQueryString() 中使用 foreach 循环即可。像这样的东西应该可以工作,注意它未经测试。:
注意:为了防止 SQL 注入,这些值应该根据所使用的数据库的适当/需要进行转义和/或引用。不要只是复制和粘贴;思考!
You're not actually imploding a multidimensional array. $conditions is an associative array.
Just use a foreach loop inside your function createQueryString(). Something like this should work, note it's untested.:
Note: To prevent SQL injection, the values should be escaped and/or quoted as appropriate/necessary for the DB employed. Don't just copy and paste; think!
未经测试,但类似的东西应该可以工作。这样你还可以检查 $item 是否是一个数组,并在这些情况下使用 IN 。
Untested, but something like this should work. This way you can also check if $item is an array and use IN for those cases.
您必须编写另一个函数来处理
$conditionalArray
,即处理$key => $value
并处理类型,例如,如果它们是字符串,则应用引号。您只是处理
=
条件吗?LIKE
、<
、>
怎么样?You will have to write another function to process the
$conditionalArray
, i.e. processing the$key => $value
and handling the types, e.g. applying quotes if they're string.Are you just dealing with
=
condition? What aboutLIKE
,<
,>
?如果不太性感请原谅我!
所以输出将是这样的:
Forgive me if its not too sexy !
So the output will be sth like :
我建议不要自动创建条件。
您的情况太本地化,而可能还有许多其他运算符 - LIKE、IN、BETWEEN、<、>等等
一些逻辑包括多个 AND 和 OR。
最好的方法是手动方式。
我总是以这种方式做这样的事情
,但是如果您仍然想要使用这个简单的数组,只需使用它进行迭代
,然后以您需要的方式组合这些变量即可。你有两个选择:使用
field='value'
对创建另一个数组,然后将其内爆,或者只是连接起来,并在末尾添加 substr 尾随AND
。I'd advise against automated conditionals creation.
Your case is too local, while there can be many other operators - LIKE, IN, BETWEEN, <, > etc.
Some logic including several ANDs and ORs.
The best way is manual way.
I am always doing such things this way
Though if you still want it with this simple array, just iterate it using
and then combine these variables in the way you need. you have 2 options: make another array of this with
field='value'
pairs and then implode it, or just concatenate, and substr trailingAND
at the end.我使用了它的变体:
它很粗糙但有效。
I use a variation of this:
It's rough but works.
这是一个工作版本:
Here is a working version:
我知道这是针对 pdo mysql 类型的情况..但我所做的是构建 pdo 包装方法,在这种情况下,我执行此函数来帮助构建字符串,因为我们使用键,所以没有可能的方法mysql 注入,因为我知道我手动定义/接受的键。
想象一下这个数据:
您定义了 utils 方法...
然后查询绑定数组生成器(我可以使用直接数组引用,但让我们简化):
然后您执行...
您也可以使用...轻松更新
I know this is for the case of a pdo mysql type.. but what i do is build pdo wrapper methods, and in this case i do this function that helps to build the string, since we work with keys, there is no possible way to mysql inject, since i know the keys i define / accept manually.
imagine this data:
you defined utils methods...
then the query bind array builder ( i could use direct array reference but lets simplify):
then you execute...
You could also go for an update easy with...
试试这个
Try this