Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL random rows query


MySQL random rows query

Reply
  #1 (permalink)  
Old 11-24-03, 08:24 PM
unclematty unclematty is offline
New Member
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL random rows query

I am looking to pull back 5 random rows in a query. Can someone please help.

I tried this but it was not what I was looking for.

select rand(products_id)
from products_desc
Reply With Quote
  #2 (permalink)  
Old 11-24-03, 09:07 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by unclematty
I am looking to pull back 5 random rows in a query. Can someone please help.
Try this:

http://www.squeekmachine.co.uk/code.php?s=5&c=94

or

http://www.desilva.biz/mysql/random.html
Reply With Quote
  #3 (permalink)  
Old 11-24-03, 09:35 PM
pack tloss pack tloss is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Try this..

PHP Code:

//select 5 random rows

$sql mysql_query("SELECT products_id FROM products_desc ORDER BY RAND LIMIT 5") or die(mysql_error());
while(
$row mysql_fetch_object($sql))
 {
  
// print product ids
  
print("$row->products_id <BR>");
 } 
Reply With Quote
  #4 (permalink)  
Old 11-25-03, 04:12 AM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 160
Thanks: 2
Thanked 0 Times in 0 Posts
Is $row->products_id the same as $row['products_id']?
__________________
Mr. Brown Eyes
Reply With Quote
  #5 (permalink)  
Old 11-25-03, 04:24 AM
fyrestrtr fyrestrtr is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
no.

if you use mysql_fetch_obj -- you will get an object in return (which is $row->products_id)

if you use mysql_fetch_array, mysql_fetch_assoc you get an array in return ( which, in case of mysql_fetch_assoc, is $row['products_id'])

So, same information, a few ways of returning it
Reply With Quote
  #6 (permalink)  
Old 11-25-03, 04:03 PM
unclematty unclematty is offline
New Member
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
thanks everyone. this did it...

SELECT products_id
FROM products_description
ORDER BY RAND( )
LIMIT 5
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
great product for dumping/recovering MySQL databases Dave Brown General Advertisements 1 10-03-03 07:40 AM
URL MySQL Query tgarske PHP 4 09-09-03 09:57 AM
creating calculated fields in mysql resource after query Abu Mami PHP 0 07-31-03 06:47 AM


All times are GMT -5. The time now is 05:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.