Problem with database search!!!
Posted: Mon Jan 20, 2003 6:01 am
I am trying to do a database search using the following script:
<html>
<head>
<title>Database Search Results</title>
</head>
<body>
<h1>Database search</h1>
<?
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Please go back and try again.";
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
@ $db = mysql_pconnect("ccc", "ccc", "ccc");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("ccc");
$query = "select * from ccc where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<p>Results: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p><strong>".($i+1).". First Name: ";
echo stripslashes($row["FirstName"]);
echo "</strong><br>Last Name: ";
echo stripslashes($row["LastName"]);
echo "<br>Phone: ";
echo stripslashes($row["Phone"]);
echo "<br>Email: ";
echo stripslashes($row["Email"]);
echo "</p>";
}
?>
</body>
</html>
This is the message I get:
Warning: Supplied argument is not a valid MySQL result resource in results.php on line 29
and this is line 29: $num_results = mysql_num_rows($result);
what can be wrong???
Thanks a lot!!
<html>
<head>
<title>Database Search Results</title>
</head>
<body>
<h1>Database search</h1>
<?
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Please go back and try again.";
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
@ $db = mysql_pconnect("ccc", "ccc", "ccc");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("ccc");
$query = "select * from ccc where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<p>Results: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p><strong>".($i+1).". First Name: ";
echo stripslashes($row["FirstName"]);
echo "</strong><br>Last Name: ";
echo stripslashes($row["LastName"]);
echo "<br>Phone: ";
echo stripslashes($row["Phone"]);
echo "<br>Email: ";
echo stripslashes($row["Email"]);
echo "</p>";
}
?>
</body>
</html>
This is the message I get:
Warning: Supplied argument is not a valid MySQL result resource in results.php on line 29
and this is line 29: $num_results = mysql_num_rows($result);
what can be wrong???
Thanks a lot!!