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"];
}
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"];
}
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.
The function mysql_fetch_object
returns an object of type \StdClass
. To return an array, use the mysql_fetch_array
function.
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
for objects use mysql_fetch_object
for array use mysql_fetch_array
for associative array mysql_fetch_assoc