2

该表Users包含数据,但仍显示Records Not Found

<?php

$conn = mysql_connect("localhost", "root", "pass", "Assign1");
$records = mysql_query($conn, "select * from Users");

if(!$records)
{
    echo "No Records Found";
    exit();
}

while($row = mysql_fetch_array($records))
{
    echo $row['name'] . " " . $row['pwd'];
    echo "<br />";
}

mysql_close($conn);

?>
4

4 回答 4

11

您有要mysql_query反转的参数。它应该是:

$records = mysql_query("select * from Users", $conn);

你的另一个问题是if声明。您正在检查if查询,而不是结果集。

另外,我敢肯定您可能知道,但是mysql库已被弃用并且正在被删除。你真的应该学会使用mysqli函数,因为它们将来对你有用得多。

链接到 MySQLi 文档- 这真的不比mysql库难。

要在正确的库中重新实现:

<?php

$mysqli = new mysqli("localhost", "user", "pass", "database");
$query = $mysqli->query("SELECT * FROM users");
$results = $query->fetch_assoc();

if($results) {
    foreach($results as $row) {
        echo $row['name'] . " " . $row['pwd'] . "<br/>";
    }
} else {
    echo "No results found.";
}

?>

希望我不只是为你完成了你的全部任务,但让另一个人正确使用 mysqli 可能是值得的。

于 2013-04-22T10:33:31.660 回答
1

mysql_query你对函数的使用有误

像这样使用它:

 <?php
$conn = mysql_connect("localhost", "root", "pass","Assign1");
$result = mysql_query("select * from Users", $conn);
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
  echo $row['name'] . " " . $row['pwd'];
  echo "<br />";
}
mysql_close($conn);
?>
于 2013-04-22T10:35:51.623 回答
0

干得好

 <?php

 $conn = mysql_connect("localhost", "root", "pass", "Assign1");
 mysql_select_db(' ----your-database-here---', $conn ) ;
 $records = mysql_query($conn, "select * from Users");

 if(mysql_num_rows($records) > 0 )
 {
 while($row = mysql_fetch_array($records))
 {
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}   


 }else
{
echo "No Records Found";
exit();
}



mysql_close($conn);

?>  
于 2013-04-22T10:48:57.367 回答
0

让我们先解决这个问题。它实际显示的错误是没有选择数据库,您必须选择需要代码的数据库

mysql_select_db("赋值1",$conn);

希望这段代码能完美解决您的问题。试一试............

 <?php
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("Assign1",$conn);
$result = mysql_query("select * from users", $conn);
if(!$result)
{
    echo "No Records Found";
    exit();
}
while($row = mysql_fetch_array($result))
{
  echo $row[0]['name'];
  echo "<br />";
}
mysql_close($conn);
?>
于 2013-04-22T10:53:44.340 回答