Page 1 of 1
MySQL query
Posted: Tue Apr 20, 2004 10:22 am
by mjseaden
Dear All,
Bit of a while since I've been using these forums, nice to be back!
Can anyone tell me what query I can use to SELECT the n-th row from a data table (i.e. only dependent on the position of the row in the table, not the row's values).
Many thanks
Mark Seaden (
mjseaden@hotmail.com)
Posted: Tue Apr 20, 2004 10:25 am
by malcolmboston
if you are using an auto-incrementing row ID
Code: Select all
$query = "SELECT * FROM mytable WHERE ID ='3'";
dont know if this is what you mean
Posted: Tue Apr 20, 2004 10:32 am
by mjseaden
Okay, change the query - the last one didn't make sense. How do I select a random row from the data table, given that I don't have an auto-incrementing row ID?
Many thanks
Mark
Posted: Tue Apr 20, 2004 10:36 am
by JayBird
to select a random row, do this query
Code: Select all
$sql = "SELECT * FROM mytable ORDER BY rand() LIMIT 1";
Mark
Posted: Tue Apr 20, 2004 10:37 am
by malcolmboston
ok, i can start it
Code: Select all
$query = "SELECT * FROM tablename";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
// now create a randomiser
$random_num = rand(1, $numrows);
$fetch_row = mysql_fetch_row($random_num)
print "$fetch_row";
now you can sort out the print statement
Posted: Tue Apr 20, 2004 10:37 am
by mjseaden
Thanks to you both
Posted: Tue Apr 20, 2004 10:38 am
by JayBird
to select a certain row in the table if you don't have an id do this
Code: Select all
$sql = "SELECT * FROM mytable LIMIT 5, 1";
This will return the 5th row. change the 5 to return whatever row you want.
Mark
Posted: Tue Apr 20, 2004 10:38 am
by malcolmboston
more efficient as usual
Posted: Tue Apr 20, 2004 2:55 pm
by Steveo31
Yea, he seems to do that ...
