0

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.

"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. Since $phpvar1 is dynamic in each situation, I do not want to store it. 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. 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"

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.

8
  • Did you executed the query.? Commented Feb 7, 2014 at 3:27
  • Are you just trying to insert the value of the PHP variables into the MySQL string? Commented Feb 7, 2014 at 3:28
  • 1
    Give this a try: "select * from `user` where `$phpvar1` in ('$phpvar2')". Also how is your $phpvar2 formed? Commented Feb 7, 2014 at 3:29
  • i don't see anything wrong with your approach. but in normal cases the we use field names instead of variable as the field names are fixed in the table. As @vee mentioned you should not use quotes around your field names Commented Feb 7, 2014 at 3:30
  • you just want to know if the 'cat' exists in the list $phpvar2? you can use a table less query SELECT 'cat' IN ('cat', 'dog') will return 1 and SELECT 'mouse' IN ('cat', 'dog') will return 0 Commented Feb 7, 2014 at 3:51

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, sorry I just noticed :)
This does not do what I want it to. MySQL is seeing $phpvar1 as a column which does not exist.
@user1965476 Ok, I got what you want, but your approach is wrong. You define temporary fields in select part of the query. Than what is your where part? It will be like "select *, `$phpvar1` from `user` where ..."
@user1965476 Maybe you simple need the following SELECT * FROM `user` WHERE `$phpvar2` = '$phpvar1' (assuming $phpvar2 is table column and $phpvar1 is query parameter to check for, if no, than I need more clarification)
@ArmanP. You may need to quote the strings IN ('".implode("', '", $phpvar2)."')
|
0

Try

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

1 Comment

No sanitizing? Tsk tsk tsk
0

Your php variable should be in the select part Select $phpvar,otherstuff, from user where x in ('$phpvar2');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.