1 Replies - 654 Views - Last Post: 24 February 2013 - 02:37 AM Rate Topic: -----

#1 Darkvortex  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-February 13

Ho to get a random row from mysql database with limit in php

Posted 23 February 2013 - 10:53 PM

Hi guys i am not know how to get a random row from mysql database in use php Like this
<?php $sql = mysql_query("SELECT * FROM users WHERE id='1'");

but i want to know how to get the row in random chance - 1/10
Is This A Good Question/Topic? 0
  • +

Replies To: Ho to get a random row from mysql database with limit in php

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: Ho to get a random row from mysql database with limit in php

Posted 24 February 2013 - 02:37 AM

<?php $sql = mysql_query("SELECT * FROM users WHERE id='1' LIMIT 4, 1");

would retrieve the 5th row (rows are indexed from 0). So construct
<?php $sql = mysql_query("SELECT * FROM users WHERE id='1' LIMIT $ran, 1");

where $ran is your random number.

Refer to my signature: the mysql library is deprecated and you should be using parameterized statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1