0

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

1
  • I use procedural since I'm used to it, if you want to give answer please give it using procedural style I haven't tried the object-oriented style yet Commented Jul 29, 2016 at 8:08

2 Answers 2

1

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>";
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You have to assign the fetch array to an array variable. I this is common both in mysql and mysqli.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.