Help: php array mysql help...

Discussion in 'PHP' started by Skillman13, Nov 21, 2009.

  1. #1
    Hi, I want this to display 4 usernames from 4 different rows in a table...

    I thought

    $query = ("SELECT * FROM `Queue` LIMIT 4");
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
    $usernameone = $row['Username'];
    $usernametwo = $row['Username'];
    $usernamethree = $row['Username'];
    $usernamefour = $row['Username'];
    echo $usernameone. $usernametwo. $usernamethree. $usernamefour;
    }

    Would do it, but it doesn't work..

    Does anyone how a way to do what i want? Or a way to fix the code above?

    Thanks alot,

    James
     
    Skillman13, Nov 21, 2009 IP
  2. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have found a way, but it is really ineffective/inefficient way, so does anyone know a better way?

    -My way does...

    $query = ("SELECT * FROM `Queue` LIMIT 4");
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    $usernameone = $row['Username'];
    echo $usernameone;

    $query = ("SELECT * FROM `Queue` LIMIT 1, 3");
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    $usernametwo = $row['Username'];
    echo $usernametwo;

    and then two times more (typing it now)
     
    Skillman13, Nov 21, 2009 IP
  3. gabibeowulfx

    gabibeowulfx Active Member

    Messages:
    211
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #3
    SELECT * FROM `Queue` LIMIT 0,4 -> that's your mysql error in the first case
     
    gabibeowulfx, Nov 21, 2009 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    1. Your query didn't use LIMIT correctly.
    2. You thought the usernames would be returned all at once in your loop.

    If you just want to display them this is how you would do it:

    
    
    $query = ("SELECT * FROM `Queue` LIMIT 0,4");
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) echo $row['Username']."<br>";
    
    
    PHP:
    If you needed to use them later on the page you should replace the echo statement inside the while loop with an array and dump them there for use later.
     
    plog, Nov 21, 2009 IP
  5. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok thanks.
     
    Skillman13, Nov 22, 2009 IP