wita
wita

Reputation: 37

mysql select query only showing one data in php

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

Upvotes: 1

Views: 1747

Answers (5)

Chirag Shah
Chirag Shah

Reputation: 1474

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

Upvotes: 1

dops
dops

Reputation: 800

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.

Upvotes: 1

Burhan &#199;etin
Burhan &#199;etin

Reputation: 676

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

        ?>

Upvotes: 1

Josef E.
Josef E.

Reputation: 2219

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!

Upvotes: 1

Chris Forrence
Chris Forrence

Reputation: 10084

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)

Upvotes: 3

Related Questions