1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What are major differences between thses lines?

Discussion in 'PHP' started by srobona, Mar 14, 2007.

  1. #1
    What are major differences among these lines?

    mysql_fetch_row();
    mysql_fetch_array();
    mysql_fetch_object();

    which one is better for what purposes?

    Will anyone reply?
    SEMrush
     
    srobona, Mar 14, 2007 IP
    SEMrush
  2. Matts

    Matts Berserker

    Messages:
    195
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I think you can find all of this in the manual at:
    http://www.php.net/manual/en/ref.mysql.php

    But the synopsis is that:
    mysql_fetch_row will return an array indexed by a numeric ordinal of the position the column is listed in the sql statement. So the first column would be at $array[0] and the second at $array[1] etc (where $array is the variable used to assign the result to).

    mysql_fetch_row will return an array indexed by both the ordinal of the position the column is listed in the sql statement *and* also by the column name. So if you did a "select id from table", you'd end up 2 elements in the array which would have indexes of 0, and 'id'. There is an optional constant that you can add to control the returned variable, the most useful of which (imo) is MYSQL_ASSOC, which will cause only the 'id' indexes to be loaded. This can save memory and cpu cycles, but my thoughts are most people don't bother with it and ignore the numeric indexes. Personally though, when I write new virgin code, I add the MYSQL_ASSOC option.

    mysql_fetch_object acts just like mysql_fetch_array($result,MYSQL_ASSOC) and instead of returning an array, returns an object with property names assigned the values of the fields in the sql statement.

    Use whatever one you are more comfortable with. The downside of mysql_fetch_row is when you add columns to a sql statement, you must add them to the end of the list or else you'll have to go through all of your code and re-index the column assignments which is a considerable pain in the neck.

    I use mysql_fetch_array because it's easy to walk through the result, and perform all kinds of interesting functions with the wide selection php array functions available, do xfoot's, etc.

    Matt
     
    Matts, Mar 14, 2007 IP
    srobona likes this.
  3. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Like you, I usually use mysql_fetch_array().

    However, thanks a lot for your valuable reply. Green rep sent :)
     
    srobona, Mar 14, 2007 IP
  4. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    mysql_fetch_row
    (PHP 3, PHP 4, PHP 5)

    mysql_fetch_row -- Get a result row as an enumerated array
    Description
    array mysql_fetch_row ( resource result )


    Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.

    Parameters


    result
    The result resource that is being evaluated. This result comes from a call to mysql_query().


    Return Values
    Returns an numerical array that corresponds to the fetched row, or FALSE if there are no more rows.

    mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

    Examples
    Example 1. Fetching one row with mysql_fetch_row()

    <?php
    $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
    if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    $row = mysql_fetch_row($result);

    echo $row[0]; // 42
    echo $row[1]; // the email value
    ?>

    ----------------------------------------------------------
    mysql_fetch_array
    (PHP 3, PHP 4, PHP 5)

    mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both
    Description
    array mysql_fetch_array ( resource result [, int result_type] )


    Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

    Parameters


    result
    The result resource that is being evaluated. This result comes from a call to mysql_query().

    result_type
    The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.


    Return Values
    Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

    If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.

    Examples
    Example 1. Query with aliased duplicate field names

    SELECT table1.field AS foo, table2.field AS bar FROM table1, table2




    Example 2. mysql_fetch_array() with MYSQL_NUM

    <?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s Name: %s", $row[0], $row[1]);
    }

    mysql_free_result($result);
    ?>



    Example 3. mysql_fetch_array() with MYSQL_ASSOC

    <?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s Name: %s", $row["id"], $row["name"]);
    }

    mysql_free_result($result);
    ?>



    Example 4. mysql_fetch_array() with MYSQL_BOTH

    <?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    printf ("ID: %s Name: %s", $row[0], $row["name"]);
    }

    mysql_free_result($result);
    ?>


    -----------------------------------------------------------


    mysql_fetch_object
    (PHP 3, PHP 4, PHP 5)

    mysql_fetch_object -- Fetch a result row as an object
    Description
    object mysql_fetch_object ( resource result [, string class_name [, array params]] )


    Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.

    Parameters


    result
    The result resource that is being evaluated. This result comes from a call to mysql_query().

    class_name
    The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.

    params
    An optional array of parameters to pass to the constructor for class_name objects.


    Return Values
    Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.

    mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

    ChangeLog


    Version Description
    5.0.0 Added the ability to return as a different object.



    Examples
    Example 1. mysql_fetch_object() example

    <?php
    mysql_connect("hostname", "user", "password");
    mysql_select_db("mydb");
    $result = mysql_query("select * from mytable");
    while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
    }
    mysql_free_result($result);
    ?>



    Example 2. mysql_fetch_object() example

    <?php

    $row = mysql_fetch_object($result);

    /* this is valid */
    echo $row->field;
    /* this is invalid */
    // echo $row->0;

    ?>
     
    jitesh, Mar 14, 2007 IP
    srobona likes this.