Jump to content


Photo

Need Help With Search Query


  • Please log in to reply
2 replies to this topic

#1 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 29 June 2006 - 12:00 PM

Hi,

I have a mysql database and want to be able to search and return results. I found an example online and have been trying to adapt it but it will not show the results. I don't know what is wrong so I would be really grateful if someone could help me out.

Here is the table:

CREATE TABLE `links` (
  `link_id` int(11) NOT NULL auto_increment,
  `category_code` varchar(10) NOT NULL default '0',
  `link_text` varchar(254) NOT NULL default '',
  `url` varchar(254) NOT NULL default '',
  `description` text NOT NULL,
  `keywords` text NOT NULL,
  `time_added` timestamp(14) NOT NULL,
  `new_window` tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (`link_id`)
) TYPE=MyISAM AUTO_INCREMENT=234;


Here is the search form:

Search<br />
<form name="search" method="post" action="srch.php"> 
<input type="text" name="find" size=16 maxlength="255"><br /> 
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>


Here is the form processing:

<?
include("common/navigation.php");
?>
            <td width="560px"><h1>Search Results</h1>

<? 
//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("localhost", "USER", "PASS") or die(mysql_error()); 
mysql_select_db("pagelinks") or die(mysql_error()); 

// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM links WHERE upper($keywords) LIKE'%$find%'"); 

//And we display the results 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['link_text']; 
echo " "; 
echo $result['description']; 
echo "<br>"; 
echo $result['url']; 
echo "<br>"; 
echo "<br>"; 
} 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?>

I have the word HTML in the keywords row of all of the links, yet when I try to search for it the following message just comes up:

Sorry, but we can not find an entry to match your query

Searched For: HTML


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 12:09 PM

you don't need to upper case trouble. Unless you tell it not to, Mysql compare string in case-insensitive by default.


change your query like this:
$data = mysql_query("SELECT * FROM links WHERE keywords LIKE '%$find%'");

Life's too short for arguing.

#3 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 29 June 2006 - 01:27 PM

Thats brilliant, thanks
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users