Jump to content

Archived

This topic is now archived and is closed to further replies.

SarahCollins

Searching through a database

Recommended Posts

I really cant see what i am doing wrong here, why i am getting errors thrown back at me.
Basically i have a form (form.html) which just has a search box and button.

<html>
<head>
<title>Search</title>
</head>
<body>
<form action="search.php" method="post">
Search: <input type="text" name="search">
<br><br><input type="submit" value="Search">
</form>
</body>
</html>

And i wish to search through a database and show all the data in it. I just want to get this bit working before i start a specific search. i have that. when i do it individually the connecting works, selecting the database doesnt throw errors, it is the 3 and 4th bit that comes back with the following error



[b]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/stud/0/a0371212/public_html/search.php on line 18[/b] (the address is my uni webpage)

[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]<?
// Connect to DB server[!--colorc--][/span][!--/colorc--]
$connection = mysql_connect("clun.scit.wlv.ac.uk", "demo", "");
if (!$connection)
die("Cannot connect to DB");

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// Select database[!--colorc--][/span][!--/colorc--][/color]
mysql_select_db("alix", $connection)
or die("Cannot find DB");
?>

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]<?
// Retrieve the data[!--colorc--][/span][!--/colorc--]
$sql = "SELECT article_title FROM articles";
$result = mysql_query($sql, $connection);

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// Loop through data and display[!--colorc--][/span][!--/colorc--]
while($a_row = mysql_fetch_assoc($result))
echo($a_row['article_title']."<BR>");
?>

<?
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// Close connection ! (please !)[!--colorc--][/span][!--/colorc--]
mysql_close($connection);
?>



any ideas?

Share this post


Link to post
Share on other sites
Try adding an "or mysql_error()" after the mysql_query() call to find out why your query is failing.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.