0

So far my code seems to work apart from one line where i get the following error:

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

Line 37 is as follows:

while($name=mysql_fetch_assoc($records))

The rest of the code:

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>

I am attempting to copy information from a database and display it on my webpage. Any help will be appreciated.

Jay Blanchard
  • 33,530
  • 16
  • 73
  • 113
  • remove mysql_connect(); after your query – Saty Apr 28 '15 at 13:14
  • try with `or die(mysql_error())` and see if any error – Dency G B Apr 28 '15 at 13:17
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 28 '15 at 13:20

1 Answers1

0

You are doing mysql_connect() twice - the second time without parameters. So you don't have a valid resource when you do the mysql_fetch_assoc().

Mel_T
  • 451
  • 5
  • 15