从SQL中检索数据

时间:2013-03-28 12:40:15

标签: php mysql mysqli

<?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中给出

3 个答案:

答案 0 :(得分: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>";
    }

?>

答案 1 :(得分:0)

在连接之前,您正在查询mysql数据库。在进行任何数据库工作之前,您必须连接到它,然后检查您是否有连接。 Error返回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标签下

答案 2 :(得分: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:查询字符串不应以分号结尾