php - Fast random row in MySql -
i need generate random row mysql database , have found illustration here: http://akinas.com/pages/en/blog/mysql_random_row/
and want utilize solution 3 looks this:
$offset_result = mysql_query( " select floor(rand() * count(*)) `offset` `users` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( " select * `users` limit $offset, 1 " ); my code looks now:
$offset_result = mysql_query( " select floor(rand() * count(*)) `offset` `users` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( "select * `users` profile_image='2' limit $offset, 1 " ); $random_date = mysql_fetch_array($result); echo $random_date['user_name']; //display username of random user but when refresh page: approximatly 7 of 10 times nonthing shows up. no username @ , im trying print out id of user it's empty. seems it's not getting @ database when refreshing , get's info database. thought why might happen?
in particular case, problem you're working 2 different queries; first query is:
select floor(rand() * count(*)) `offset` `users` whereas sec 1 includes condition:
select * `users` profile_image='2' limit $offset, 1 ^^^^^^^^^^^^^^^^^^^^^^^ both queries should operate on same result set.
php mysql database
No comments:
Post a Comment