简体   繁体   中英

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

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 )

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

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!

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

        ?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM