Difference between two functions

Discussion in 'PHP' started by computerzworld, Aug 24, 2007.

  1. #1
    Hi. I want to know the difference between two functions of php. One is mysql_fetch_array() and another one is mysql_fetch_object(). I want to know the basic difference between these two and how they are working? Please tell me friends. Thanks in advance.
     
    computerzworld, Aug 24, 2007 IP
  2. xemiterx

    xemiterx Peon

    Messages:
    62
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From php.net:

    Note: mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).

    If you had a database with the first field as 'Name' and used both ways:

    $obj = mysql_fetch_object($result);
    $ary = mysql_fetch_array($result);

    You could display the value of 'Name' like this:
    echo $obj->Name;
    echo $ary[0];
    echo $ary['Name'];

    Also from php.net:

    Performance: Speed-wise, mysql_fetch_object() is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).
     
    xemiterx, Aug 24, 2007 IP
    computerzworld likes this.
  3. DKameleon

    DKameleon Member

    Messages:
    29
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #3
    It's simple. First obe returns an array, and the second one returns an object :)

    http://www.php.net/mysql_fetch_array
    http://www.php.net/mysql_fetch_object

    Note the difference in examples:
    print $row->name;
    Code (markup):
    and
    print $row["name"];
    Code (markup):
     
    DKameleon, Aug 24, 2007 IP
    computerzworld likes this.
  4. computerzworld

    computerzworld Active Member

    Messages:
    214
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    thanks you have given the perfect solution.........
    thanks again..........
     
    computerzworld, Aug 24, 2007 IP