| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Member Join Date: Aug 2005
Posts: 125
![]() | 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!
__________________ upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google! Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly! |
| |
| | #2 (permalink) |
| DNOA Member Join Date: May 2004
Posts: 5,040
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | I actually wrote one for my old website (www.vibewave.com) that retrieves a random quote... Here: PHP Code: |
| |
| | #3 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,552
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Easy: select [field] from [table] order by rand() limit 1 and you fetch one random record at a time
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists |
| |
| | #5 (permalink) | ||||
| Senior Member Join Date: Jul 2004 Location: Kizmiaz
Posts: 1,091
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Lux
__________________ 1 Cent Web Hosting @ Online HostGatorVoucher.com Adsense Hack for Vbulletin - Fully 3.6 Compatible ForexArea.com Free E-Course in Programming EA's | ||||
| |
| | #6 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,552
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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.
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists |
| |
| | #7 (permalink) | ||||
| NamePros Regular Join Date: Aug 2005
Posts: 315
![]() | 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
????: NamePros.com http://www.namepros.com/showthread.php?t=135721 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-n...ws-from-table/
__________________
Last edited by tgo; 10-30-2005 at 09:39 AM.
| ||||
| |
| | THREAD STARTER #8 (permalink) | ||||
| NamePros Member Join Date: Aug 2005
Posts: 125
![]() |
PHP Code: I'm sure I did everything right (including connecting to the database). To prove it: when I replace PHP Code: PHP Code: Just wondering...how do I fix the error? Thanks!
__________________ upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google! Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly! | ||||
| |
| | THREAD STARTER #10 (permalink) |
| NamePros Member Join Date: Aug 2005
Posts: 125
![]() | Thanks for you help compuXP! the script I used: PHP Code:
__________________ upload² |AIMForum.com - Ready, AIM, Chat!|Google Talk Forum - Where the world talks about Google! Forum Elves - Quality Forum Posting At Affordable Prices! -|Order A Package|PaidPosts.com Help Boost Your Forum|BannerManage.com Sell direct advertisements on your website effortlessly! |
| |