Citra45Abadi Citra45Abadi - 4 months ago 10
PHP Question

Cannot use object of type mysqli_result as array error

I just learned mysqli since Mysql_ queries is deprecated and people keep telling me to use mysqli instead

so for starter I make a simple code to connect, insert and show data
I have no problem making connection, inserting data to database but I cannot show the data using mysqli_fetch_array

here is my code :

<?php
$sql=("SELECT * from data_orang");
$hasil=mysqli_query($con,$sql);
while(mysqli_fetch_array($hasil)){
echo "nama : $hasil[nama] <br>
umur : $hasil[umur] <br>
kelamin : $hasil[kelamin] <br>";
}
?>


this is what I've tried

echo " nama : $hasil['nama'] <br>
umur : $hasil['umur'] <br>
kelamin : $hasil['kelamin'] <br>


I also tried adding
mysqli_assoc
and
mysqli_free_result($hasil)
but it does not work

Answer

As its says: "Cannot use object of type mysqli_result as array error". You have to create an array from result.

while($result = mysqli_fetch_array($hasil)){
  echo "nama : $result[nama] <br>
      umur : $result[umur] <br>
      kelamin : $result[kelamin] <br>";
  }
Comments