Jump to content

building a mysql query using php


ajoo

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.