SQL表数据未出现在页面上

时间:2013-07-28 11:31:17

标签: php sql

简单的一点,我在使用php在网页上显示我的sql数据时遇到了困难。我有一个单独的页面连接到数据库。但是使用简单的连接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>";

    }

    ?>

1 个答案:

答案 0 :(得分:1)

请检查您是否在connect.php中正确编写了所有连接变量。您正在使用$rows[$id]这不是问题,因为您已定义变量$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 />";
}
?>