0

简单的一个,我的 sql 数据没有显示在使用 php 的网页上时遇到了困难。我有一个单独的页面,它按照连接到数据库。然而,使用简单的连接 php 脚本不会显示结果......

<?php
include("header.php");
include("connect.php");
?>



<?php

    $sql = mysqli_query("SELECT * FROM members ORDER BY id ASC");

    $id = 'id';
    $username = 'username';
    $useremail = 'useremail';
    $rows = mysqli_fetch_assoc($sql);

    echo 'Name: ' . $rows[$id] . '<br />' . 'Username: ' . $rows[$useremail];

    ?>

我什至试过这个,但仍然没有......

<?php

    include 'includes/connect.php';


    $query = "SELECT * FROM members";

    $result = mysqli_query ($query);

    while($person = mysqli_fetch_array($result)) {

        echo "<h1>" . $person['useremail'] . "</h1>";

    }

    ?>
4

1 回答 1

1

请检查您是否在connect.php. 您正在使用$rows[$id]which 没有问题,因为您已经定义了 variable $id='id'。但问题是你没有使用循环。如果你要打印

$rows = mysqli_fetch_assoc($sql);
echo $rows[$id];

它只会给出一条记录;请尝试以下

<?php
$host='localhost';
$user='root';
$pass='';
$db='mydata';//change above four variable as per your setting or write down in connect.php
$con=mysqli_connect($host,$user,$pass) or die(mysqli_error());
mysqli_select_db($con,$db);
$id = 'id';
$username = 'username';
$useremail = 'useremail';
$q="SELECT * FROM members ORDER BY id ASC";
$r=mysqli_query($con,$q);
while($rows=mysqli_fetch_assoc($r)){
    echo "Name :".$rows[$username]."<br />"."useremail :".$useremail."<br />";
}
?>
于 2013-07-28T12:29:25.600 回答