1

so I'm trying to show data from my sql table. There are two data but only one of them shows when I try to open my php page. here's my codes :

<?php
    include ("koneksi.php");

    // $username=$_GET['username'];
    $username="Dia";

    $result = mysql_query("SELECT * FROM tbl_penyakit_user WHERE username='Dia'");
    $row = mysql_fetch_array( $result );

    echo " penyakit: ".$row['penyakit'];

?>

I tried to run the select query on phpmyadmin an it showed 2 data. Thanks in advance

Josef E.
  • 2,179
  • 4
  • 15
  • 30
wita
  • 37
  • 7

5 Answers5

3

mysql_fetch_array only gets one array at a time. To properly get all available rows, put it in a while-loop like so:

while($row = mysql_fetch_array($result)) {
    echo " penyakit: " . $row['penyakit'];
}

As an aside, however, please note that the mysql_* functions are considered deprecated, and should not be used in future development. (Here's why)

Community
  • 1
  • 1
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
1

You have to use while loop while more than one row.

<?php
    include ("koneksi.php");
    // $username=$_GET['username'];
    $username="Dia";

    $result = mysql_query("SELECT * FROM tbl_penyakit_user WHERE username='Dia'");
    while($row = mysql_fetch_array( $result ))
    {
        echo " penyakit: ".$row['penyakit'];
    }
?>
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
1

Try this:

$result = mysqli_query($con,"SELECT * tbl_penyakit_user WHERE username='Dia'");

    while($row = mysqli_fetch_array($result)) {
      echo $row['penyakit'];
    }

Hope this helps!

Josef E.
  • 2,179
  • 4
  • 15
  • 30
1
    <?php
 include ("koneksi.php");

// $username=$_GET['username'];
 $username="Dia";

 $result = mysql_query("SELECT * FROM tbl_penyakit_user WHERE username='Dia'");
 while($row = mysql_fetch_assoc( $result )){
     echo " penyakit: ".$row['penyakit'];
}

        ?>
Burhan Çetin
  • 676
  • 7
  • 16
1

You need to fetch each row so use a while loop

while ($row = mysql_fetch_array( $result )) {
    echo " penyakit: ".$row['penyakit'];
}

This should output all rows, you only fetch one.

dops
  • 800
  • 9
  • 17