Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 16th, 2004, 07:10 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
Selecting Random Database Item

I am trying to write a script that will connect to my MySQL Database and select a random entry in a table and then print it to the screen.

Here is the my database table setup
php Code:
Original - php Code
  1.  
  2.   `id` int(11) NOT NULL default '0',
  3.   `name` varchar(40) NOT NULL default '',
  4.   `description` text NOT NULL,
  5.   `upright` text NOT NULL,
  6.   `reversed` text NOT NULL,
  7.   `image` varchar(40) NOT NULL default '',
  8.   `uthumb` varchar(40) NOT NULL default '',
  9.   `rthumb` varchar(40) NOT NULL default '',

Here is my script code
php Code:
Original - php Code
  1.  
  2. $db = "listing";
  3. $con = @mysql_connect("localhost","username","password");

I need the script to connect to the listing database and randomly select an entry from id table and print it in the script. Each entry has information like this..

php Code:
Original - php Code
  1.  
  2. INSERT INTO `database name` VALUES (1, 'text description 1 rn', 'text description 2 rn', 'text description 3', 'pic1.jpg', '/thumbs/pic1.gif', '/thumbs/pic1r.gif');


Ok for example my script connects to the database and I need it to randomly select 9 different entries. How do I have it to randomly select the 9 entries and echo them to the page?

Any Help is very much apperiated.

Reply With Quote
  #2  
Old March 16th, 2004, 09:02 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: Selecting Random Database Item

The easiest way to randomly select rows from a table is by ordering by RAND() and using a LIMIT statement.
Code:
SELECT * FROM table_name ORDER BY RAND() LIMIT 9


To output the data from the query, use something like this:
php Code:
Original - php Code
  1.  
  2. $sql = "SELECT * FROM table_name ORDER BY RAND() LIMIT 9";
  3. $result = mysql_query($sql, $con);
  4. while($row = mysql_fetch_assoc($result))
  5. {
  6.   foreach($row as $column => $value) echo "$column = $value<br>n";
  7.   echo "<br>n";
  8. }

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Selecting Random Database Item


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT