PHP/MySql Search problem
Posted: Tue Sep 16, 2003 3:27 pm
Hi, I am trying to create an page that shows up form, and when the user enters a search, refreshs, without the form, and shows up the results. I have all of that working (Thanks to the guy who helped me with that). The problem I have it that if there are no results, the page shows up blank. I want it to show up "Sorry, no answer" but it dosen't.
The script works fine if there are results to display
I know the search is looking for exact matchs, which is as it should be, which is why I need to let people know there is no results.
Thanks for any help - James
The script works fine if there are results to display
Code: Select all
<?php
if ($_GET['c'] != 1)
{
?>
<form name="form1" action="search2.php" method="GET">
<input type="hidden" name="c" value="1">
<input name="search" type="text" id="search" value="Enter search here" size="50">
<input type="submit" value="Search!">
</form>
<?php
} else if ($_GET['c']==1) {
$dbcnx = @mysql_connect("localhost", "root",
"mypasswd");
//Variable with database connection created
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}//Error message set
//Selecting correct database
if (! @mysql_select_db("jamesforum_uk_db") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
$result = @mysql_query("SELECT * FROM ansr_info WHERE answer LIKE '$search'");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
while ( $row = mysql_fetch_array($result) ){
if (!$result) {
echo ("Sorry, no answer");
}
$answer = $row["answer"];
echo ("The answer to your question is: " . $answer . "<P>");
}
}
?>Thanks for any help - James