PHP/MySql Search problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

PHP/MySql Search problem

Post by Linkjames »

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

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>");
}
}
?>
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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Hints...

Code: Select all

// check how many rows are returned from the database...
if (mysql_num_rows($result) == 0) { echo 'No hits'; }
Try to fit that in somewhere, and change it around to fit your needs.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

You sir, are my hero. Thank you very much
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Quick add on question, does anyone know a way to make the search ignore case? (Caps and so on)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, it is on default usage.

Code: Select all

mysql> SELECT 'abc' LIKE 'ABC';
        -> 1
mysql> SELECT 'abc' LIKE BINARY 'ABC';
        -> 0
As you are allready using LIKE, I would not think that you need to change anything. On the other hand:

Code: Select all

mysql> SELECT 'abc' LIKE 'B';
        -> 0
mysql> SELECT 'abc' LIKE '%B%';
        -> 1
...using % you get wildcards (can be changed to _ (underscore) to fit a single character).

http://www.mysql.com/doc/en/Fulltext_Search.html might also be helpful to you, regarding searching text.
Post Reply