Results from the most recent live auction are here .
17 members in the live chat room. Join Chat !
10-28-2005, 01:12 PM
· #1 NamePros Member
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (
Donate )
Randomingly choosing a quote from Database, PHP
I just started PHP and I'm still trying to figure somethings out.
Could anyone provide a code for randomly choosing and displaying a quote from a database (two columns: quote, author)?
Thanks!
10-28-2005, 01:47 PM
· #2 DNOA Member
Name: Matthew Holt
Location: 127.0.0.1
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (
Donate )
I actually wrote one for my old website (
www.vibewave.com ) that retrieves a random quote...
Here:
PHP Code:
//get a random quote... using arrays.
//Be sure to connect to the db.
$query1 = "SELECT * FROM quotes" ;
$result1 = mysql_query ( $query1 );
while ( $row1 = mysql_fetch_array ( $result1 ))
{
$id1 = $row1 [ 'id' ];
$array1 [ count ( $array1 ) + 1 ] = $id1 ;
shuffle ( $array1 );
}
shuffle ( $array1 );
//ACCESS THE RANDOM NUMBER IS BY $array1[0]
//Now we have the id of the random quote.
$random = $array1 [ 0 ];
$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1" ;
$r1 = mysql_query ( $q1 );
while ( $row1 = mysql_fetch_array ( $r1 ))
{
$content = $row1 [ 'content' ];
$author = stripslashes ( $row1 [ 'author' ]);
}
//Then just echo $content and $author (the person who said it) when you need it.
?>
It may not be efficient but it works
__________________
FREE : Help With Code
Includes other technical topics:
programming, development, Windows, domain names, and Internet
10-28-2005, 05:56 PM
· #3 Law and disorder
Name: Kate
Location: Expat
Join Date: Aug 2005
Posts: 5,133
NP$: 895.11 (
Donate )
Easy:
select [field] from [table]
order by rand()
limit 1
and you fetch one random record at a time
10-28-2005, 08:20 PM
· #4 DNOA Member
Name: Matthew Holt
Location: 127.0.0.1
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (
Donate )
Unfortunately, wouldn't rand() return any number, not just those in the db?
__________________
FREE : Help With Code
Includes other technical topics:
programming, development, Windows, domain names, and Internet
10-28-2005, 08:31 PM
· #5 Senior Member
Name: Lux Interior
Location: Kizmiaz
Join Date: Jul 2004
Posts: 1,090
NP$: 637.00 (
Donate )
Originally Posted by compuXP Unfortunately, wouldn't rand() return any number, not just those in the db?
No it wouldn't and yes that is the correct way to do it.
Lux
10-29-2005, 07:15 AM
· #6 Law and disorder
Name: Kate
Location: Expat
Join Date: Aug 2005
Posts: 5,133
NP$: 895.11 (
Donate )
Order by Rand() just means "order the resultset in random order" (instead of ASC or DESC for example) so you won't get a record ID which is not part of the resultset.
10-30-2005, 08:31 AM
· #7 NamePros Regular
Join Date: Aug 2005
Posts: 317
NP$: 102.00 (
Donate )
Rand is not the best way but it works.
If its a small database then go for it, but if its more than 100 records dont use that way, the reason is ORDER BY RAND() forces Mysql to generate alot of random numbers.
Quote from below resource
Quote: What happens when you run such a query? Let’s say you run this query on a table with 10000 rows, than the SQL server generates 10000 random numbers, scans this numbers for the smallest one and gives you this row. Generating random numbers is relatively expensive operation, scaning them for the lowest one (if you have LIMIT 10, it will need to find 10 smallest numbers) is also not so fast (if quote is text it’s slower, if it’s something with fixed size it is faster, maybe because of need to create temporary table).
I code ASP and dont code PHP so I cant give you any code but I think you would be better off with this type of thing
SELECT COUNT(*) FROM DBNAME
get the total records here
Close recordset
calculate your random number between 1 and the total records.
I dont know PHP dont take this literaly
srand((double)microtime()*1000000);
$iRandom = rand(0,$totalrecords);
Now that you have a random number you can use the LIMIT in the Query properly.
SELECT [field] FROM [table] LIMIT $randomvariable, 1
The above limits the rows to one, and starts at the random number
Please dont cut paste my code here as i stated I code ASP and my PHP here is most likely wrong.
For more info on this
http://www.titov.net/2005/09/21/do-...ows-from-table/
Last edited by tgo : 10-30-2005 at 08:39 AM .
10-30-2005, 06:56 PM
· #8 NamePros Member
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (
Donate )
Quote: PHP Code:
//get a random quote... using arrays.
//Be sure to connect to the db.
$query1 = "SELECT * FROM quotes" ;
$result1 = mysql_query ( $query1 );
while ( $row1 = mysql_fetch_array ( $result1 ))
{
$id1 = $row1 [ 'id' ];
$array1 [ count ( $array1 ) + 1 ] = $id1 ;
shuffle ( $array1 );
}
shuffle ( $array1 );
//ACCESS THE RANDOM NUMBER IS BY $array1[0]
//Now we have the id of the random quote.
$random = $array1 [ 0 ];
$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1" ;
$r1 = mysql_query ( $q1 );
while ( $row1 = mysql_fetch_array ( $r1 ))
{
$content = $row1 [ 'content' ];
$author = stripslashes ( $row1 [ 'author' ]);
}
//Then just echo $content and $author (the person who said it) when you need it.
?>
It may not be efficient but it works
it's returning this error aoubt how in
PHP Code:
while ( $row1 = mysql_fetch_array ( $r1 ))
$r1 is not a valid MySQL resource for mysql_fetch_array
I'm sure I did everything right (including connecting to the database). To prove it: when I replace
PHP Code:
$q1 = "SELECT * FROM quotes WHERE id='$random' LIMIT 1" ;
with
PHP Code:
$q1 = "SELECT * FROM quotes WHERE 1 LIMIT 1" ;
It returns with my first quote.
Just wondering...how do I fix the error? Thanks!
10-30-2005, 07:26 PM
· #9 DNOA Member
Name: Matthew Holt
Location: 127.0.0.1
Join Date: May 2004
Posts: 4,867
NP$: 17.21 (
Donate )
Apparenlty the variable $random doesn't contain a value, or contains an invalid one. Tweak the script by putting in echo($random); then an exit; statement on the next line. Tell me what shows up.
__________________
FREE : Help With Code
Includes other technical topics:
programming, development, Windows, domain names, and Internet
10-31-2005, 05:50 AM
· #10 NamePros Member
Join Date: Aug 2005
Posts: 125
NP$: 512.00 (
Donate )
Thanks for you help compuXP!
the script I used:
PHP Code:
$q1 = "SELECT * FROM quotes ORDER BY rand() LIMIT 1" ;
$r1 = mysql_query ( $q1 );
$row1 = mysql_fetch_array ( $r1 );
$content = $row1 [ 'quote' ];
$author = stripslashes ( $row1 [ 'author' ]);
echo "<font size=\"-1\"><i>" . $content . "</i><br>" ;
echo "<div align=right><b>" . $author . "</b></div></font>" ;
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off