ukscotth Posted November 3, 2011 Share Posted November 3, 2011 Hi, This is probably very easy for someone who knows how but I cant seem to get it working. Can someone please tell me how I can alter this query to select a 'WHERE gender = female' so instead of picking a random user it picks a random female user ? $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $user1b = mysql_query( " SELECT * FROM `users` LIMIT $offset, 1 " ); $user1= mysql_fetch_array($user1b); Also, would it be easy to modify it so it picks a second random user that isnt the same as the first user ? so I end up with 2 random females ( $user1 and $user2 ) Many thanks, Scott Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/ Share on other sites More sharing options...
ukscotth Posted November 3, 2011 Author Share Posted November 3, 2011 also need to add ' WHERE uid != $user' somehow. Any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/#findComment-1284757 Share on other sites More sharing options...
cyberRobot Posted November 3, 2011 Share Posted November 3, 2011 Can someone please tell me how I can alter this query to select a 'WHERE gender = female' so instead of picking a random user it picks a random female user ? The WHERE clause would go after the FROM: <?php $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE gender='female'"); //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/#findComment-1284808 Share on other sites More sharing options...
cyberRobot Posted November 3, 2011 Share Posted November 3, 2011 also need to add ' WHERE uid != $user' somehow. Any ideas ? Which query is that supposed to be added to? Note that the following description of the WHERE clause might be helpful: http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/#findComment-1284809 Share on other sites More sharing options...
ukscotth Posted November 4, 2011 Author Share Posted November 4, 2011 Thanks for your reply. Its working now, I swear thats what I tried before but I must of been doing something wrong. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/#findComment-1284810 Share on other sites More sharing options...
cyberRobot Posted November 4, 2011 Share Posted November 4, 2011 No problem, glad to hear it's working. Based on the original post: ...query to select a 'WHERE gender = female' so instead... Maybe the first attempt didn't include the quotes around female... For example, this won't work: <?php $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE gender=female"); //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/#findComment-1284885 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.