本文介绍了sql pdo php where 在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用变量从我的数据库中选择一些数据.
I'd like to select some data from my database using variables.
这是我的代码的简短版本:
Here is a short version of my code:
// $a is either null or something like [1, 2]
if ($a) {
$debug = implode(',', $a);
} else {
$debug = [0-9];
}
$sql = "SELECT id FROM user WHERE id IN ($debug)"
如果设置了 $a
和所有用户如果 $a,我怎样才能实现我只得到用户 1 和 2(=
没有设置?$a
中的值)
How can I achieve that I get only user 1 and 2 (= value in $a
) if $a
is set and all user if $a
is not set?
推荐答案
第一:
当你在查询中直接注入字符串时要注意,因为你可能成为 SQL 注入的目标
First:
be aware when you directly inject string inside queries, because you can be target of a SQL Injection
第二个:
直接更改查询可能是最简单的解决方案
Second:
change directly the query might be the easiest solution
// $a is either null or something like [1, 2]
$sql = "SELECT id FROM user";
if ($a) {
$debug = implode(',', $a);
$sql = "SELECT id FROM user WHERE id IN ($debug)";
}
这篇关于sql pdo php where 在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!