Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/
Share on other sites

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'");
//...
?>

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

No problem, glad to hear it's working.  8)

 

 

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");
//...
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.