简体   繁体   中英

Query always returns recently added data only

I'm trying to show all my data from the database I made in mysql.

I am using this code:

<table border= "3">
    <tr>
        <th>ID</th>
        <th>Game Name</th>
    </tr>

    <?php
    $query = "SELECT * FROM `test_game_name`";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)) {
        $id = $row['game_id'];
        $name = $row['game_name'];
    }
    ?>

    <tr>
        <td><?php echo $id; ?></td>
        <td><?php echo $name; ?></td>
    </tr>
</table>

My problem is that not all the data show up, only the data I recently added. I believe that SELECT * means selecting all the data.

But I don't know what's the problem why it does not show all the data, anyone would happen to know?

You need to add your td inside your while loop

<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
?>
<tr>
    <?php
    while ($row = mysql_fetch_array($result)) {
        $id = $row['game_id'];
        $name = $row['game_name'];
        echo " <td>" . $id . "</td>";
        echo " <td>" . $name . "</td>";
    }
    ?>
</tr>

Note:- mysql is deprecated instead use mysqli and PDO

add this inside while,

<tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $name; ?></td>
</tr>

Final Code

while($row = mysql_fetch_array($result)) {
    $id = $row['game_id'];
    $name = $row['game_name']; ?>

   <tr>
      <td><?php echo $id; ?></td>
      <td><?php echo $name; ?></td>
   </tr>
<?php } ?>

Switch to mysqli_* or PDO instead of mysql_* which is deprecated.

If you are not connected with database then follow this code it will help you.

<table border= "3">
        <tr>
            <th>ID</th>
            <th>Game Name</th>
        </tr>

        <?php
        $link=mysql_connect("localhost", "root","");
        mysql_select_db('dbname', $link);
        $query = "SELECT * FROM `test_game_name`";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result)) {?>
             <tr>
                 <td><?php echo $row['game_id']; ?></td>
                 <td><?php echo $row['game_name']; ?></td>
             </tr>
       <?php }
        ?>

    </table>

Try using this:

<table border= "3">
<tr>
       <th>ID</th>
        <th>Game Name</th>
 </tr>
 <?php
 $query = "SELECT * FROM `test_game_name`";
 $result = mysql_query($query); 
 while($row = mysql_fetch_array($result))
 {
     $id = $row['game_id'];
     $name = $row['game_name'];
 ?>
     <tr>
         <td><?php echo $id; ?></td> 
         <td><?php echo $name; ?></td> 
     </tr>
 <?php } ?>
 </table>

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