darknessmdk Posted June 8, 2007 Share Posted June 8, 2007 I need help with a select statement. My statement is $result = mysql_query("SELECT * FROM properties WHERE city = '$city' AND beds LIKE '%$beds%' AND baths LIKE '%$baths%' OR state = '$state' AND beds LIKE '%$beds%' AND baths LIKE '%$baths%' OR zip = '$zip' AND beds LIKE '%$beds%' AND baths LIKE '%$baths%' ORDER BY date DESC LIMIT 0,8"); What I am trying to do is select rows by state, city or zip and those results are further narrowed by how many bedrooms and baths the user selects. The problem If the user just wants out a city or state and ignores the beds and baths, it doesnt return any results. I want it to. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/ Share on other sites More sharing options...
TheLostGuru Posted June 8, 2007 Share Posted June 8, 2007 I'm not sure if this will help, I'm pretty new to PHP, but couldn't you make a default beds and baths, so that if the user didn't select anything it just defaulted to whatever value you give it? Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-270904 Share on other sites More sharing options...
darknessmdk Posted June 8, 2007 Author Share Posted June 8, 2007 You would think that would work wouldnt you. I set the default to 0 and it searchs for properties with 0 beds and 0 baths Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-270909 Share on other sites More sharing options...
darknessmdk Posted June 11, 2007 Author Share Posted June 11, 2007 Can anyone help with this? Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-272306 Share on other sites More sharing options...
chrisuk Posted June 11, 2007 Share Posted June 11, 2007 perhaps a long winded way but a possible stop gap would be to have a seperate select statement that is executed if only the city is entered...so along the lines of if city is not empty AND beds OR baths are empty, just select from the DB where city matches the user input Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-272312 Share on other sites More sharing options...
Psycho Posted June 11, 2007 Share Posted June 11, 2007 You need to create your query dynamically based upon the user input. Plus, that query is inefficient as you are repeatedly restating the clauses for beds and baths. You just need to put the locations within parenthetical operators. Here is an example, but I would add a lot more error handling and validation of the variables. <?php $query = "SELECT * FROM properties WHERE (city = '$city' OR state = '$state' OR zip = '$zip') "; $query .= ($beds!='')? "AND beds LIKE '%$beds%' " : '' ; $query .= ($baths!='')? "AND baths LIKE '%$baths%' " : '' ; $query .= "ORDER BY date DESC LIMIT 0,8"; $result = mysql_query($query) or DIE (mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-272318 Share on other sites More sharing options...
darknessmdk Posted June 11, 2007 Author Share Posted June 11, 2007 I didnt know you could break up a query like that, never seen that before Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-272319 Share on other sites More sharing options...
Psycho Posted June 11, 2007 Share Posted June 11, 2007 Break it up which way exactly? Do you mean using the parenthetical operators or the dynamic creation of the AND clauses? Let me know if you have any questions. Otherwise, if this works for you please mark the post as solved. Quote Link to comment https://forums.phpfreaks.com/topic/54772-solved-select-statement-help/#findComment-272362 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.