-2
<?php
$sql = mysql_query("SELECT * FROM users;");

    while($row = mysql_fetch_array($sql))
    {
    $dbCon=mysqli_connect("localhost", "root", "", "dbusers")
       or die(mysqli_error()."Connection disconnected");
    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>
  • 我的代码有什么问题?

这是我总是得到的错误:警告:mysql_fetch_array() 期望参数 1 是资源,在第 4 行的 C:\xampp\htdocs\CRUD\CRUD_Act\includes\dbdisp.php 中给出的布尔值

4

3 回答 3

1

先连接数据库!

<?php
 $dbCon=mysql_connect("localhost", "root", "", "dbusers")
       or die(mysql_error()."Connection disconnected");
$sql = mysql_query("SELECT * FROM users;");

    while($row = mysql_fetch_array($sql))
    {

    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>
于 2013-03-28T12:42:20.203 回答
0

正如他们所说:你必须连接到数据库

包括您的文件连接:

喜欢 :

include("connexion.php");

或者像他们建议的那样做,因为你在连接到它之前查询了 ​​mysql 数据库。

接着 :

  <?php
   $sql ="SELECT * FROM users";
   $res=mysql_query($sql) or die("erreur");
    while($row=mysql_fetch_row($res))
    {
    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>

PS:查询字符串不应以分号结尾

于 2013-03-28T12:50:35.467 回答
0

在连接到它之前,您正在查询 mysql 数据库。在你做任何数据库工作之前,你必须连接到它,然后检查你是否有连接。错误返回 false,因为它无法运行查询,因为没有要查询的数据库。

<?php
$dbCon=mysql_connect("localhost", "root", "", "dbusers")
   or die(mysql_error()."Connection disconnected");
$sql = mysql_query("SELECT * FROM users;");

while($row = mysql_fetch_array($sql))
{
    echo "<tr>";
    echo "<td>" . $row['UserID'] . "</td>";
    echo "<td>" . $row['Firstname'] . "</td>";
    echo "<td>" . $row['Lastname'] . "</td>";
    echo "<td>" . $row['Gender'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td>" . $row['Status'] . "</td>";
    echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
}

这应该在 php 标签下

于 2013-03-28T12:42:39.817 回答