Citra45Abadi Citra45Abadi - 2 months ago 6x
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 :

$sql=("SELECT * from data_orang");
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
but it does not work


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>";