![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| Hello Everyone, I am building an application that will need to select 6 random records from a MySQL table. I'm not quite sure how to do this. Currently, I'm doing it this way: each record has a unique ID that numbers from 1 to X. I get the MAX number from the unique ID field and assign a PHP variable a random number between 1 and the MAX number. Then I just do a SELECT * FROM table WHERE uniqueID = $random_var. I put this in a 6X executing loop. But this isn't elegant and I want to replace it with the correct way. Can anyone help me out? Thanks, Anthony Saffer -- Professional Software and Web Application Development www.safferconsulting.com | |||
|
| Advertisements |
| | #2 | ||
| On 13-Jul-2003, "Anthony M. Saffer" <anthony@nospam.safferconsulting.com> wrote: > I am building an application that will need to select 6 random records > from > a MySQL table. I'm not quite sure how to do this. Currently, I'm doing it > this way: each record has a unique ID that numbers from 1 to X. I get the > MAX number from the unique ID field and assign a PHP variable a random > number between 1 and the MAX number. Then I just do a SELECT * FROM table > WHERE uniqueID = $random_var. I put this in a 6X executing loop. But this > isn't elegant and I want to replace it with the correct way. Can anyone > help > me out? You are making it way too hard, try this: SELECT * FROM table ORDER BY RAND() LIMIT 6 You don't need id numbers or multiple selects. -- Tom Thackrey www.creative-light.com | |||
|
| | #3 | ||
| "Tom Thackrey" <tomnr@creative-light.com> wrote in message news:HynQa.983$L%7.357@newssvr23.news.prodigy.com. .. > > On 13-Jul-2003, "Anthony M. Saffer" <anthony@nospam.safferconsulting.com> > wrote: > > > I am building an application that will need to select 6 random records > > from > > a MySQL table. I'm not quite sure how to do this. Currently, I'm doing it > > this way: each record has a unique ID that numbers from 1 to X. I get the > > MAX number from the unique ID field and assign a PHP variable a random > > number between 1 and the MAX number. Then I just do a SELECT * FROM table > > WHERE uniqueID = $random_var. I put this in a 6X executing loop. But this > > isn't elegant and I want to replace it with the correct way. Can anyone > > help > > me out? > > You are making it way too hard, try this: > SELECT * FROM table ORDER BY RAND() LIMIT 6 > You don't need id numbers or multiple selects. Thank you very much! I WAS making it way too hard. Anthony Saffer | |||
|
| | #4 | ||
| On 15-Jul-2003, Job <jobjes@hotmail.com> wrote: > Tom Thackrey wrote: > > On 13-Jul-2003, "Anthony M. Saffer" > > <anthony@nospam.safferconsulting.com> > > wrote: > > > > > > You are making it way too hard, try this: > > SELECT * FROM table ORDER BY RAND() LIMIT 6 > > You don't need id numbers or multiple selects. > > > > > Hi, was thinking about this myself, but am wondering: > This returns 6 items from table, but are these 6 unique returns, or can > this also result in duplicate returns? No. Think of it as: assign each row a random number, sort by that number and pick the first 6 rows. -- Tom Thackrey www.creative-light.com | |||
|
| Featured Websites | ||||
|
![]() |
| Tags: mysql, question |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OT: MySQL question | Chris | PHP | 2 | 07-01-2007 3:34 PM |
| newbie PHP Forms Mysql question. | MeNotHome | PHP | 0 | 07-01-2007 3:15 PM |
| Mysql commercial licence question | Adam Lipscombe | Database | 0 | 06-10-2007 12:23 AM |
| phpdev mysql question | Greg | Database | 1 | 06-10-2007 12:23 AM |
| MySQL Query Cache Not Working: MySQL 5 / Windows XP | Good Man | Database | 0 | 05-31-2007 8:46 PM |
| Featured Websites | ||||
|