Error in mysql_fetch_object

5

I do not know what happens in this error.

  

Fatal error: Can not use object of type stdClass as array in

$result = mysql_query("select * from usuarios");
while ($row = mysql_fetch_object($result)) {
    echo $row["Id"];
    echo $row["Nome"];
}
    
asked by anonymous 18.02.2014 / 19:15

4 answers

5

To access the return from mysql_fetch_object() use this syntax:

$row->nome_da_chave;

If you want to use associative arrays use mysql_fetch_assoc() , to access the items just

$row['nome_da_chave'];

We strongly recommend using the PDO or mysqli to connect to the database, since the mysql_* functions have already been deprecated and will soon be removed.

Reasons not to use the mysql functions _ *

    
18.02.2014 / 19:21
1

The function mysql_fetch_object returns an object of type \StdClass . To return an array, use the mysql_fetch_array function.

    
18.02.2014 / 19:16
1

When you use mysql_fetch_object the return of the pointer is always an object of type stdClass, so you can not reference it with the brackets because only arrays are referenced so for reference you should use mysql_fetch_assoc or mysql_fetch_array.

//utilizando mysql_fetch_assoc
$result = mysql_query("select * from usuarios");
while ($row = mysql_fetch_assoc($result)) {
    echo $row["Id"];
    echo $row["Nome"];
}

//utilizando mysql_fetch_object
$result = mysql_query("select * from usuarios");
while ($row = mysql_fetch_object($result)) {
    echo $row->Id;
    echo $row->Nome;
}

documentation link

    
18.02.2014 / 19:17
0

for objects use mysql_fetch_object for array use mysql_fetch_array for associative array mysql_fetch_assoc

    
25.02.2014 / 21:13