SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fetch_row and fetch_array

    I commonly use mysql_fetch_row();

    When I run this command within a while statement it dumps the row into $result (array)

    What is the difference between using fetch_row and the other methods of retrieving response? such as fetch_array? And what are all the fetch options I have to choose from?
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual

  2. #2
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There isn't much difference between mysql_fetch_array and mysql_fetch_row, except mysql_fetch_array was designed to get multiple rows, eg.

    PHP Code:
    $sql mysql_query("SELECT column FROM table...") or die (mysql_error());

    while (
    $row mysql_fetch_array($sql)) {
    extract($row);
    $something[] = $column;

    That will store all the rows found into the array $something.

    I ususally just use mysql_fetch_array since if there's only one result, the speed difference isn't noticeable between fetch array and fetch row.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_array and mysql_fetch_row do exactly the same thing. There is no difference. In fact the only difference is fetch_row returns the array with numerical indices like

    PHP Code:
    while($row mysql_fetch_row($result)) {
    print 
    $row[0];

    While mysql_fetch_array() returns both numerical and named indices like:

    PHP Code:
    while($row mysql_fetch_row($result)) {
    print 
    $row['fieldname'];
    //or
    print $row[0];
    }
    //these will prdocue the same results. 

    On a side note mysql_fetch_array() takes an optional param of what kind of result to return.


    PHP Code:
    mysql_fetch_row($resultMYSQL_ASSOC)
    //Returns only named indices 
    PHP Code:
    mysql_fetch_row($resultMYSQL_NUM)
    //Returns only numbered indices 
    PHP Code:
    mysql_fetch_row($resultMYSQL_BOTH)
    //Returns both same as
    mysql_fetch_row($result
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy,

    in your examples there:

    PHP Code:
    while($row mysql_fetch_row($result)) {
    print 
    $row['fieldname'];
    //or
    print $row[0];
    }
    //these will prdocue the same results. 
    Don't you mean this to be:

    PHP Code:
    while($row mysql_fetch_array($result)) {
    print 
    $row['fieldname'];
    //or
    print $row[0];
    }
    //these will prdocue the same results. 
    Or did you mean to put mysql_fetch_row?
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes dkode, it's supposed to be fetch_array() as fetch_row() only makes numerical indexes. just a mistake...
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for catching that, mistake on my part. Should have been array like Dr. said.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •