This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                4年前关闭。
            
                    
到目前为止,我的代码似乎可以从以下一行工作:


  警告:mysql_fetch_assoc()期望参数1为资源,在第37行的C:\ xampp \ htdocs \ Website \ Search.php中给出布尔值


第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()时,您没有有效的资源。

10-04 22:36