Sunden Sunden - 4 years ago 115
SQL Question

Displaying Multiple rows with MySQL while loop

I'm trying to make it so that this code shows however many rows there are that match up with the owner. The owner that I'm using to test currently has three items but only one of the items shows up. How can I make it so that it displays all of the owner's items?

<?php
$username = $_SESSION['username'];
$get = @mysql_query("SELECT * FROM items WHERE owner = '$username'");
$row = mysql_fetch_assoc($get);
$check = mysql_num_rows($get);

if($check == 0)
echo "You have no items in your inventory.";

while($row = mysql_fetch_assoc($get))
{
echo "<p>Item Name:" . $row['name'] . "</p></br>";
}
?>

Answer Source

Do it like this

<?php
    $username = $_SESSION['username'];
    $get = @mysql_query("SELECT * FROM items WHERE owner = '$username'");
    // deleted
    $check = mysql_num_rows($get);

    if($check == 0)
        echo "You have no items in your inventory.";

    while($row = mysql_fetch_assoc($get))
    {
        echo "<p>Item Name:" . $row['name'] . "</p></br>";
    }
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download