Irish SEO,  Marketing & Webmaster Discussion

 

mySQL / PHP / selecting 1 random row

This is a discussion on mySQL / PHP / selecting 1 random row within the Coding Help forums, part of the Webmaster Help category; I am looking for a nice way to do the following , in PHP / mySQL Select a random row ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Calendar Search Today's Posts Mark Forums Read

  #1 (permalink)  
Old 04-08-2008, 03:47 PM
paul's Avatar
ninja SEO
 
Join Date: Dec 2006
Location: .de
Posts: 1,251
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
Question mySQL / PHP / selecting 1 random row

I am looking for a nice way to do the following , in PHP / mySQL
  1. Select a random row from a database , with a certain dateType
  2. Assign the values read out to variables
Database has columns, dateType, date1 , date2, date3

PHP Code:
  "SELECT * FROM reserved dates WHERE dateType='2'
ORDER BY RAND()
LIMIT 1"
;
 = 
mysql_query();
while( = 
mysql_fetch_array(, MYSQL_ASSOC))
{
    echo 
"Date1: <br>" .
         
"Date2:  <br>" .
         
"Date3:  <br><br>";

I was just wondering if there is a more graceful way of doing i, rather than doing a while loop there ? Plus the query is probably very slow they way I have it. Maybe I will just use this : http://www.greggdev.com/web/articles.php?id=6
__________________
my sites :
irish poker / irish jobs / seo faq / advertise jobs free / green card / skiing

Last edited by paul; 04-08-2008 at 03:53 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 04:17 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

you don't need the while as you are only selecting 1 row.
PHP Code:
 sSql "SELECT * FROM reserved dates WHERE dateType='2' ORDER BY RAND() LIMIT 0,1";
rs mysql_query(sSql);
row mysql_fetch_array(rsMYSQL_ASSOC);
    echo 
"Date1: <br>" .
         
"Date2:  <br>" .
         
"Date3:  <br><br>"
__________________
:. Web Design & Development Web Design Ireland
:. Search Engines Optimization Search Engines Optimization
:. Car Parts & Accessories Car Parts
:. Cars Ireland Cars Ireland
:. I Have 2 Find It Directory SEF Directory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 04:58 PM
paul's Avatar
ninja SEO
 
Join Date: Dec 2006
Location: .de
Posts: 1,251
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
Default

Cheers Louie ! The names of the columns .ie date1, date2, date3 will be accessible under $rs[date1]; ?

BTW. this vbulletin seems to strip the $ bits.

Last edited by paul; 04-08-2008 at 05:01 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 05:14 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

no it should be under "row['date1']"

Yes I know. Michelle should do something about it...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 05:55 PM
paul's Avatar
ninja SEO
 
Join Date: Dec 2006
Location: .de
Posts: 1,251
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
Default

Oops that was I meant to put in.

As for the error here I think it's something to do with that plugin for youTube.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
mysql, php, random, row, selecting

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Commit a Random act of Kindness Now 3rsales Work / Life Balance 15 08-01-2009 04:49 PM
My random Game of the day... Forbairt Off topic discussion 0 19-02-2008 11:49 PM
My random ECard post of the day .. Forbairt Off topic discussion 2 18-02-2008 09:52 AM
[PHP] How to create a random array? babyboy808 Coding Help 7 29-12-2007 11:18 PM
Just a random question, Joseph Grogan Off topic discussion 16 17-11-2007 09:28 PM


Sponsored links

Pepperjam Network
Paid On Results www.zanox.com


All times are GMT +1. The time now is 06:32 PM.


Powered by: vBulletin Version 3.8.2, Copyright ©2000 - 2009, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger

Search Engine Friendly URLs by vBSEO 3.3.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51