ajoo Posted January 28, 2014 Share Posted January 28, 2014 Hi all ! I wish to build a mysql query which is very simple. An example of the query I want is as follows: $query = "Select * FROM $table WHERE tables=1 OR tables=2 AND Member_status = 'D' "; The problem is that the conditions Where tables=1 or tables=2 can vary and i can have tables=3 , tables=4 etc. as well. So basically the OR condition is ramdom and must be constructed in a loop. I constructed a loop which stores the result in a variable $tablescnt. i.e. echo .$tablescnt; gives tables=1 OR tables=2 OR tables=3 and so on depending upon the loop iterations. However i don't know i am going to substitute this ($tablescnt) into $query above. I tried $query = "Select * FROM $table WHERE '$tablescnt' AND Member_status = 'D' "; where i hoped that $tablescnt would expand as desired. However it did not work. So any suggestions on how I may proceed would be very helpful. Thanks all for your time to check this out and help me. Link to comment https://forums.phpfreaks.com/topic/285752-building-a-mysql-query-using-php/ Share on other sites More sharing options...
requinix Posted January 28, 2014 Share Posted January 28, 2014 What you've described sounds like it would work (it's a little vague). What's your code? Link to comment https://forums.phpfreaks.com/topic/285752-building-a-mysql-query-using-php/#findComment-1466880 Share on other sites More sharing options...
ginerjm Posted January 28, 2014 Share Posted January 28, 2014 Leave the quotes off the substituted variable. Link to comment https://forums.phpfreaks.com/topic/285752-building-a-mysql-query-using-php/#findComment-1466882 Share on other sites More sharing options...
ajoo Posted January 29, 2014 Author Share Posted January 29, 2014 Hi guys ! thanks for both the replies. Ginerjim, you were right. leaving the quotes off the variable did the trick. Surprising thing is that I tried that before posting this question and for some reason it did not work. After your suggestion I tried it again just now and it worked !! Thanks loads both of you. Very grateful. Ajoo. Link to comment https://forums.phpfreaks.com/topic/285752-building-a-mysql-query-using-php/#findComment-1466900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.