doubi9615 2017-05-18 17:47
浏览 167

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>
  • 写回答

2条回答 默认 最新

  • doushe2513 2017-05-18 17:53
    关注

    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();
    ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭