SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ORDER BY results into variables

    I am using ORDER BY id DESC LIMIT 0,2 to select the 2 most recent entries in my database where the id column is an auto increment, like this.

    $recent = mysql_query('SELECT column_name FROM table ORDER BY id DESC LIMIT 0, 2');
    while ($recentEntry = mysql_fetch_array($recent)) {
    echo $recentEntry['column_name'];
    }

    My question is how can I place the 2 retrieved entries into seperate variables, so that the first entry is placed into $entry1 and the second into $entry2.

    Thanks

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try the following:

    PHP Code:
    $recent mysql_query('SELECT column_name FROM table ORDER BY id DESC LIMIT 0, 2');

    for( 
    $i 1$i <= mysql_num_rows($recent); $i++ )
    ${
    'entry'.$i} = mysql_result($recent$i-1'column_name'); 
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's how I would do it...

    PHP Code:
    $recent mysql_query('SELECT column_name FROM table ORDER BY id DESC LIMIT 0, 2');
    for(
    $i=1;$recentEntry mysql_fetch_array($recent);$i++) {
    $varname='entry'.$i;
    $
    $varname=$recentEntry['column_name'];

    Edit:

    Haha... Pretty much the same.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers, I am a newbie for some reason was only able to get Young Twig's loop to work.

    When I tried this elsewhere on my page only the first character in the string is being displayed
    <?php echo $entry1['column_name']; ?>

    how do I get the whole string as the result? I realize this is probably a simple thing. but can't figure it out.

    Thanks again

  5. #5
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait a minute, do you want the entire row as an array? Like this:

    Code:
    $entry1
    Array (
    column_name => value,
    column_name => value,
    column_name => value
    )
    
    $entry2
    Array (
    column_name => value,
    column_name => value,
    column_name => value
    )
    If so, just do this:
    PHP Code:
    $recent mysql_query('SELECT column_name FROM table ORDER BY id DESC LIMIT 0, 2'); 
    for(
    $i=1;$recentEntry mysql_fetch_array($recent);$i++) { 
    $varname='entry'.$i
    $
    $varname=$recentEntry


  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Very Much, I Got it Now

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, "LIMIT 0,2 " is strictly equivalent to "LIMIT 2" so you can type two chars less.
    Second, if you know exactly how many entries you have, there is no need for loops:

    PHP Code:
    $rc mysql_query('SELECT column_name FROM table ORDER BY id DESC LIMIT 2');
    $entry1 mysql_fetch_array($rc);
    $entry2 mysql_fetch_array($rc); 


Bookmarks

Posting Permissions

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