+ Reply to Thread
Results 1 to 5 of 5

Thread: mySQL / PHP / selecting 1 random row

  1. #1
    paul's Avatar
    paul is offline ninja SEO paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,277

    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
    Last edited by paul; 04-08-2008 at 03:53 PM.

  2. #2
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    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

  3. #3
    paul's Avatar
    paul is offline ninja SEO paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,277

    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.

  4. #4
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

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

    Yes I know. Michelle should do something about it...
    :. 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

  5. #5
    paul's Avatar
    paul is offline ninja SEO paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,277

    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.

+ Reply to Thread

Similar Threads

  1. Commit a Random act of Kindness Now
    By 3rsales in forum Work / Life Balance
    Replies: 15
    Last Post: 08-01-2009, 05:49 PM
  2. My random Game of the day...
    By Forbairt in forum Off topic discussion
    Replies: 0
    Last Post: 20-02-2008, 12:49 AM
  3. My random ECard post of the day ..
    By Forbairt in forum Off topic discussion
    Replies: 2
    Last Post: 18-02-2008, 10:52 AM
  4. [PHP] How to create a random array?
    By babyboy808 in forum Coding Help
    Replies: 7
    Last Post: 30-12-2007, 12:18 AM
  5. Just a random question,
    By Joseph Grogan in forum Off topic discussion
    Replies: 16
    Last Post: 17-11-2007, 10:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO

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 52 53 54 55 56 57 58 59 60 61 62 63 64