表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);
?>
答案 0 :(得分:11)
您的参数mysql_query
已反转。它应该是:
$records = mysql_query("select * from Users", $conn);
您的另一个问题是if
声明。您在查询时检查if
,而不是在结果集上检查。
此外,我相信您可能知道,但mysql
库已被弃用且正在删除。您应该学会使用mysqli
函数,因为它们将来会对您有用。
Link to MySQLi documentation - 它实际上并不比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可能是值得的。
答案 1 :(得分: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);
?>
答案 2 :(得分: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);
?>
答案 3 :(得分:0)
让我们先解决这个问题。实际显示的错误是没有选择数据库你必须选择需要代码的数据库
mysql_select_db( “为Assign1”,$ 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);
?>