micmola Posted March 25, 2011 Share Posted March 25, 2011 Is it possible to have a loop inside a mysql select statement? If yes, can someone please give me an idea. Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2011 Share Posted March 25, 2011 Without an example of what you are trying to accomplish, the answer would be that almost anything is possible in programming, as long as it makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192166 Share on other sites More sharing options...
DavidAM Posted March 25, 2011 Share Posted March 25, 2011 ... as long as it makes sense. I'd strike that last part; I've seen a lot of things done in programming that made absolutely no sense. Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192173 Share on other sites More sharing options...
micmola Posted March 25, 2011 Author Share Posted March 25, 2011 An example of having a for loop inside a mysql SELECT statement is below: $query = "SELECT * FROM car WHERE offer_type = '" . $transaction_type . for ($x=0; x<count($sentence_array); $x++) { "' AND car_name = '" . $sentence_array[$x] . "' OR car_color = '" . $sentence_array[$x] . "' OR car_seats = '" . $sentence_array[$x] . } "'"; The sentence_array contains one word per element. Please someone help me on this. Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192176 Share on other sites More sharing options...
DavidAM Posted March 25, 2011 Share Posted March 25, 2011 You want to build a query string using a for loop, not actually run a for loop inside a SELECT statement. Actually, you can do it without a for loop, just use implode(). Something like this should work: $query = "SELECT * FROM car WHERE offer_type = '" . $transaction_type . " AND "; $query .= "( car_name IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= " OR car_color IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= " OR car_seats IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= ')'; Note: Untested. (I don't have an environment here to test with). Note: Make sure you sanitize those inputs if they are coming from a user. (see mysql_real_escape_string()) Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192193 Share on other sites More sharing options...
sasa Posted March 25, 2011 Share Posted March 25, 2011 change implode("' OR '", $sentence_array) to implode("','", $sentence_array) Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192239 Share on other sites More sharing options...
DavidAM Posted March 25, 2011 Share Posted March 25, 2011 Oops , Thanks sasa. I was originally going a different route and didn't think to change that. Quote Link to comment https://forums.phpfreaks.com/topic/231687-having-a-for-loop-inside-a-mysql-select-statement/#findComment-1192244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.