简体   繁体   English

在mysql select语句中使用两个php变量

[英]Utilizing two php variables in mysql select statement

I am not sure if I am apporaching this in the best way, but what I am trying to do is use two php variables for a IN clause in a mysql select query. 我不确定我是否以最好的方式使用它,但我想要做的是在mysql选择查询中使用两个php变量用于IN子句。

"select * from `user` where '$phpvar1' IN ('$phpvar2')"

Normally instead of $phpvar1 I would use a column name in the user table and it works fine, however in my case now I need the $phpvar1 variable to be used as a temporary column in this specific select query. 通常代替$ phpvar1我会在user表中使用列名并且它工作正常,但在我的情况下,我现在需要将$ phpvar1变量用作此特定选择查询中的临时列。 Since $phpvar1 is dynamic in each situation, I do not want to store it. 因为$ phpvar1在每种情况下都是动态的,所以我不想存储它。 What can I do? 我能做什么? Thanks! 谢谢!

EDIT: To clarify, $phpvar1 is not a column in the table, but I want it to act as one only for this one query. 编辑:为了澄清, $phpvar1不是表中的列,但我希望它只作为这一个查询的一个。 In this query I want $phpvar1 which is equal to for example: "cat", where as "cat" is the data that would be found in the column, and then this data ("cat") would be used in the IN clause to see if $phpvar2 contains "cat" 在这个查询中,我想要$phpvar1 ,它等于例如:“cat”,其中“cat”是可以在列中找到的数据,然后这个数据(“cat”)将在IN子句中使用看看$phpvar2包含“cat”

Basically what I want it to do is this: 基本上我想要它做的是这样的:

SELECT * from `users` where 'mouse' IN ('cat', 'dog') and `userID` = '1'

the rows returned should have a userID = 1 AND meet the IN clause, since 'mouse' is not in ('cat,'dog') there won't be any rows returned in this case. 返回的行应该有一个userID = 1并且符合IN子句,因为'mouse'不在('cat,'dog')中,在这种情况下不会返回任何行。

Rewrite your query to: 将您的查询重写为:

"SELECT * FROM `users` WHERE '$phpvar1' IN ('".implode("', '", $phpvar2)."') and `userID` = '1'"

Assuming that $phpvar2 is array containing values to check for. 假设$phpvar2是包含要检查的值的数组。

尝试

 $sql = "select * from `user` where ".$phpvar1." in (".$phpvar2.")";

你的php变量应该在select部分中选择$ phpvar,otherstuff,来自用户,其中x in('$ phpvar2');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM