PHP mysql SELECT QUERY 带有 Or

发布于 2024-12-17 05:31:58 字数 184 浏览 1 评论 0原文

mysql_query("SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2");

这是行不通的。

基本上,我希望能够在 id 是一个变量的值或另一个变量的值的情况下选择它。

谢谢。

编辑:ID 列是数字。

mysql_query("SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2");

This doesn't work.

Basically, I want to be able to select it where the id is one variable's value OR another one's.

Thanks.

EDIT: The ID column is numerical.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所的.畏惧 2024-12-24 05:31:58

正如其他人所说并且您确认的那样,问题在于您正在使用字符串文字与数字列进行比较。为了让它工作,查询应该看起来像

mysql_query("SELECT * FROM foo WHERE id =$foo OR id = $foo2");

但是,这个解决方案有非常非常糟糕的代码味道!

首先,这就是为什么IN 存在:能够写入

mysql_query("SELECT * FROM foo WHERE id IN ($foo, $foo2)");

其次,您是否将未转义的字符串注入您的查询?如果是,您的代码很容易受到 sql 注入!为了安全起见,转义并引用你的变量,像这样(在一般情况下):

$query = sprintf("SELECT * FROM foo WHERE id IN ('%s', '%s')",
                 mysql_real_escape_string($foo),
                 mysql_real_escape_string($foo2));
mysql_query($query);

或者像这样,因为在这个特定的场景中你知道我们正在谈论整数值:

$query = sprintf("SELECT * FROM foo WHERE id IN (%s, %s)",
                 intval($foo), intval($foo2));
mysql_query($query);

脚注:我知道,当像这样使用 sprintf 时,还可以通过仅使用 %d 而不是 if %s 作为格式说明符来处理整数值。但是,我相信只要查看一个地方(参数列表)而不是多个地方(我是否在变量上使用了 intval ?或者也许我没有),就可以证明您正确地转义了变量,但我在格式字符串中使用 %d 所以我仍然没问题?)。这听起来可能违反直觉,但在面对修改时它更加稳健。

As others have said and you confirmed, the problem is that you are using string literals to compare to a numeric column. To have it work, the query should look like

mysql_query("SELECT * FROM foo WHERE id =$foo OR id = $foo2");

However, this solution has very very bad code smell!

First off, this is why IN exists: to be able to write

mysql_query("SELECT * FROM foo WHERE id IN ($foo, $foo2)");

And second, are you injecting unescaped strings into your query? If you are, your code is vulnerable to sql injection! Escape and quote your variables to be safe, like this (in the general case):

$query = sprintf("SELECT * FROM foo WHERE id IN ('%s', '%s')",
                 mysql_real_escape_string($foo),
                 mysql_real_escape_string($foo2));
mysql_query($query);

or alternatively like this, since in this specific scenario you know we 're talking about integer values:

$query = sprintf("SELECT * FROM foo WHERE id IN (%s, %s)",
                 intval($foo), intval($foo2));
mysql_query($query);

Footnote: I am aware that when using sprintf like this, one could also handle integer values by just using %d instead if %s as the format specifier. However, I believe that proving you are correctly escaping variables should be possible by just looking at one place (the parameter list) instead of multiple places (did I use intval on the variable? or maybe I did not, but I 'm using %d in the format string so I 'm still OK?). It may sound counter-intuitive, but it's more robust in the face of modifications.

浅笑轻吟梦一曲 2024-12-24 05:31:58

我认为您忘记了最后一个 ' 字符

mysql_query("SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2'");

,但由于 id 列是数字,您应该使用:

mysql_query("SELECT * FROM foo WHERE id = $foo OR id = $foo2");

I think you forgot the last ' character

mysql_query("SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2'");

but because the id column is numerical, you should use:

mysql_query("SELECT * FROM foo WHERE id = $foo OR id = $foo2");
放血 2024-12-24 05:31:58

试试这个:

mysql_query(sprintf("SELECT * FROM foo WHERE id = %s OR id = %s", $foo, $foo2));

我建议你使用 mysql_error() 来获取 mysql 错误(如果存在)。

mysql_query( .. ) or die('Erro:'.mysql_error());

mysql_error 返回 mysql 中发生的最后一个错误。

Try this:

mysql_query(sprintf("SELECT * FROM foo WHERE id = %s OR id = %s", $foo, $foo2));

I recommend you use mysql_error() for get mysql errors(if exists).

mysql_query( .. ) or die('Erro:'.mysql_error());

the mysql_error returns the last error occurred in mysql.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文