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

4
  • You need to fetch the second row - this is typically done in a loop Commented May 1, 2014 at 14:56
  • you need a while loop to iterate over the array Commented May 1, 2014 at 14:58
  • 1
    If any answer really helpful to you than please accept it. Commented May 1, 2014 at 15:00
  • Also you could consider looking into PDO in PHP. One of my personal favorites when working wih databases. Commented May 1, 2014 at 15:21

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

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'];
}

        ?>

Comments

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.

Comments

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'];
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.