andy_b_1502 Posted May 21, 2012 Share Posted May 21, 2012 Hi everyone! I need to know how to write two $what_services variables in so it looks for both "R" AND "B".. R and B are values within my db table, here's the code: <?php $what_services = "R"; $query = "SELECT * FROM companies WHERE what_services = '$what_services' ORDER BY approved DESC, company_name ASC"; $result = mysql_query($query); ?> iv'e tried the following but have gotten undesired results: two what services like so; $what_services = "R"; $what_services = "B"; separating with comma's; $what_services = "R, B"; Using AND; $what_services = "R AND B"; None of which works, what is the correct way to write this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/ Share on other sites More sharing options...
Kristoff1875 Posted May 21, 2012 Share Posted May 21, 2012 Try what_services== Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/#findComment-1347272 Share on other sites More sharing options...
andy_b_1502 Posted May 21, 2012 Author Share Posted May 21, 2012 like this: $what_services== "R", "B"; Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/#findComment-1347273 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2012 Share Posted May 21, 2012 You want rows where what_services = 'R' or where what_services = 'B'. There's two ways - A) $query = "SELECT * FROM companies WHERE what_services = 'R' OR what_services = 'B' ORDER BY approved DESC, company_name ASC"; B) $query = "SELECT * FROM companies WHERE what_services IN('R','B') ORDER BY approved DESC, company_name ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/#findComment-1347274 Share on other sites More sharing options...
andy_b_1502 Posted May 21, 2012 Author Share Posted May 21, 2012 Thanks, Which one will work best with my while loop do you think? <?php $what_services== "R"; $query = "SELECT * FROM companies WHERE what_services = '$what_services' ORDER BY approved DESC, company_name ASC"; $result = mysql_query($query); while ( $row = mysql_fetch_array($result)) { $approved = $row['approved']; $what_services = $row['what_services']; Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/#findComment-1347275 Share on other sites More sharing options...
andy_b_1502 Posted May 21, 2012 Author Share Posted May 21, 2012 $query = "SELECT * FROM companies WHERE what_services IN('R','B') ORDER BY approved DESC, company_name ASC"; worked as expected, many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/262869-select/#findComment-1347278 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.