从数据库获取信息时出现 PHP 错误

标签 php html mysql

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


6年前关闭。




到目前为止,我的代码似乎与我收到以下错误的一行不同:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Website\Search.php on line 37



第 37 行如下:
while($name=mysql_fetch_assoc($records))

其余代码:
HEAD> <TITLE> Search </TITLE> </HEAD>
<BODY>

<h1>Search for your favourite movies here</h1>
<b>`enter code here`
<p> Here you can search through our exciting selection of existing and upcoming movies </p>
<p> You can search our website by choosing from one of the drop down lists below depending on what you
    know about the movie </p>
</b>


<?php

mysql_connect('localhost', 'root', '');

mysql_select_db('database');
$sql="SELECT * FROM names";

$records=mysql_query($sql);
mysql_connect();

?>
<html>
<body>

<table width="600" border="1" cellpadding="1" cellspacing="1">
</table>

<tr>
<th>fname</th>
<th>sname</th>
<th>age</th>
<tr>

<?php

while($name=mysql_fetch_assoc($records))
{
    echo "<tr>";

    echo "<td>.$name.['fname'].</td>";
    echo "<td>.$name.['sname'].</td>";
    echo "<td>.$name.['age'].</td>";

    echo "</tr>";
}


?>

</BODY>
</HTML>

我正在尝试从数据库中复制信息并将其显示在我的网页上。任何帮助将不胜感激。

最佳答案

你在做mysql_connect()两次 - 第二次没有参数。
因此,当您执行 mysql_fetch_assoc() 时,您没有有效的资源。 .

关于从数据库获取信息时出现 PHP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29920563/

相关文章:

html - 在 jQuery Mobile 环境中将表放入表单中的问题

mysql - 如何与毫秒一起检索 DATETIME

php - 为什么 PHP 的下拉列表中 mysql 数据库的值没有设置为 "selected"

mysql - 查询执行成功但在表中插入了其他数据

php - Yii - 如何打印 findAll 使用的 SQL

php - Laravel Paypal - 从网站传递送货地址

php - 使用 PHP 自动递增数组值

PHP unicode解码

javascript - 每次保存文档时,换行符和段落都会增加一倍 ckeditor 在每次保存后添加额外的空格段落和换行符

javascript - html 每行代码换行