I am trying to link my website to my database for the results but it shows these errors:
Warning: mysql_select_db() expects parameter 1 to be string, resource given in C:\wamp\www\SearchEngine\connect.php on line 9
and
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\SearchEngine\search.php on line 44
These are the codes for those 2 files:
connect.php:
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
echo "Cannot connect to database";
die();
}
mysql_select_db($con,"SearchEngine");
?>
search.php:
<?php
//php code goes here
include 'connect.php'; // for database connection
$query = $_GET['q'] // query
?>
<html>
<head>
<title>
Brandon's Search Engine
</title>
<style type="text/css">
#search-result {
font-size: 22;
margin: 5px;
padding: 2px;
}
#search-result:hover {
border-color: red;
}
</style>
</head>
<body>
<form method="GET" action="search.php">
<table>
<tr>
<td>
<h2>
Brandon's Search Engine
</h2>
</td>
</tr>
<tr>
<td>
<input type="text" value="<?php echo $_GET['q']; ?>" name="q" size="80" name="q"/>
<input type="submit" value="Search" />
</td>
</tr>
<tr>
<td>
<?php
//SQL query
$stmt = "SELECT * FROM web WHERE title LIKE '%$query%' OR link LIKE '%$query%'";
$result = mysql_query($stmt);
$number_of_result = mysql_num_rows($result);
if($number_of_result < 1)
echo "Your search did not match any documents. Please try different keywords.";
else
{
//results found here and display them
while($row = mysql_fetch_assoc($result))
{
$title = $row["title"];
$link = $row["link"];
echo "<div id='search-result'>";
echo "<div id='title'" . $title . "</div>";
echo "<br />";
echo "<div id='link'" . $link . "</div>";
echo "</div>";
}
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>
If possible, please explain to me.