PHP:使用mysql数组时如何输出多行?

I am currently programming a system to tell users bans from a database. However, It is only returning one result.

How would I change this to echo more than one result?

<?php 
    include("check.php");     
?>

<?php
mysql_connect("localhost", "root", "[PASSWORD]") or die(mysql_error()); 
mysql_select_db("bans") or die(mysql_error()); 
$query= mysql_query("SELECT * FROM bans WHERE name = '".$_SESSION['username']."' ")or die(mysql_error());
$arr = mysql_fetch_array($query);
$num = mysql_numrows($query); //this will count the rows (if exists) 
?> 
 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Home</title> 
<link rel="stylesheet" href="style.css" type="text/css" /> 
</head> 
 
<body> 
<h1 class="hello">Hello, <em><?php echo $login_user;?>!</em></h1> 
 
<a href="logout.php" style="font-size:18px">Logout?</a> 
</body> 
</html>

<html>
<?php if($num > 0){ ?>
<h1>Your Punishments</h1>
<p>Type: <?php echo $arr['type']; ?></p>
<p>Reason: <?php echo $arr['reason']; ?></p>

<?php }else{ ?>
 User not found.
<?php } ?>

</html>

</div>

check this code, this is demo code from

https://www.w3schools.com/php/php_mysql_select.asp

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?> 

Msql library is deprecated and removed as of PHP 7.0.0. php.net says:

mysql_connect — Open a connection to a MySQL Server

Warning: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

  • So, use mysqli library. Is very easy to make the changes in your code: replace mysql_ with mysqli_ overall. Almost 98% enough.
  • In your PHP: you should loop through database results and save them in an array ($bans). This way, you can close your db connection immediately after fetching. All steps further you'll make only on the array, without needing to worry about the database anymore.
  • See that your HTML syntax is always correct! You have 2 x <body> in your page.
  • All steps are described by the commented code.
  • Use intention-revealing variable (and function) names.
  • Don't write text direct in <body>. Use <span>, <div>, etc to encapsulate it.

<?php
// Fetch the user name, always assign default, even if NULL again.
// Like so, you can move it later in a function.
$userName = isset($_SESSION['username']) ? $_SESSION['username'] : NULL;

// Create an array to hold the query results.
$bans = array();

// Connect to db.
$connection = mysqli_connect('localhost', 'root', '[PASSWORD]', '[DB]');
if (!$connection) {
    die('Connect error: ' . mysqli_connect_errno() . ' >> ' . mysqli_connect_error());
}

// Run query on db.
$sql = "SELECT * FROM bans WHERE name = '" . $userName . "'";
$queryBans = mysqli_query($connection, $sql);

// Loop through results and add each record to $bans array.
// I recommend you to fetch what you need from db, 
// save in arrays and close db connection immediately.
while ($row = mysqli_fetch_array($queryBans, MYSQLI_ASSOC)) {
    $bans[] = $row;
}

// Close connection.
mysqli_close($connection);

// Count bans.
$numBans = count($bans);
?> 

<!DOCTYPE HTML> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>Home</title> 
        <link rel="stylesheet" href="style.css" type="text/css" /> 
    </head> 

    <body> 
        <h1 class="hello">Hello, <em><?php echo $login_user; ?>!</em></h1>

        <a href="logout.php" style="font-size:18px">Logout?</a>

        <?php
        if ($numBans > 0) {
            // Loop through $bans and display.
            foreach ($bans as $ban) {
                ?>
                <h1>Your Punishments</h1>
                <p>Type: <?php echo $ban['type']; ?></p>
                <p>Reason: <?php echo $ban['reason']; ?></p>
                <?php
            }
        } else {
            ?>
            <span>User not found.</span>
            <?php
        }
        ?>
    </body>
</html>