Jump to content

[SOLVED] SQL SELECT + random


chocopi

Recommended Posts

How would i go about selecting information from a table and then picking a random one out of the selected

 

<?php
$1 = mysql_query("SELECT id FROM Mark WHERE id > 0");
rand($1);
?>

 

or something like that

 

Thanks,

 

~ Chocopi

Link to comment
Share on other sites

Another one ...

 

<?

$query = mysql_query ("SELECT id,name FROM table WHERE id>0");

 

$rand = rand(0,10);

 

$result = mysql_result($query,$rand,"name");

 

echo $result;  // this will display a random name from the table with names.

?>

Link to comment
Share on other sites

cheers taith but it wasnt working so i put mysql_error in there and it returned:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

 

egorig, thanks i shall try yours in a mo

 

~ Chocopi

Link to comment
Share on other sites

thanks for all your help guys, but

 

this is what i ended up with:

 

<?php
$query = mysql_query("SELECT id FROM table WHERE id>0 ORDER BY rand() LIMIT 1");
$row = mysql_fetch_assoc($query) or die(mysql_error());
$blah = $row['id'];
echo $blah;
?>

 

However it is only returning 1 and none of the other values

 

~ Chocopi

Link to comment
Share on other sites

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.