如何在MySQL表中查找特定单词?

i want to find specific keyword in MySQL Table ....

The main code is:

if ($_REQUEST["keyword"]<>'') {
$search_string = "AND trainer like '".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR venue like '".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR session like '".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR course like '".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR category like '".mysql_real_escape_string($_REQUEST["keyword"])."%'";    
} 
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." 
WHERE id>0".$search_string;
}

The problem is the code above shows same result for any word searched. Any help of reference will be much appreciated.

Assuming your problem is that you're getting incorrect results, you can fix this with the following:

$search_string = " AND (trainer like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
    OR venue like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
    OR session like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
    OR course like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
    OR category like '%".mysql_real_escape_string($_REQUEST["keyword"])."%')";    

Changes made:

  1. Added brackets around your set of OR statements
  2. Prepended the wildcard character to your search string, as well as the existing appended wildcard
  3. Added a space before your AND so your resulting query doesn't end up containing id>0AND

Edit:

I thought I could help more by explaining what is going wrong. Assuming someone searches for 'foo' and your table is called 'bar', your code will generate the following query:

SELECT
*
FROM
bar
WHERE
id>0AND
trainer like 'foo%'
OR venue like 'foo%'
OR session like 'foo%'
OR course like 'foo%'
OR category like 'foo%'

There are a couple of problems here:

  1. You have combined your parts incorrectly so AND winds up right next to 'id>0', a space before AND fixes that problem.
  2. You haven't wrapped your set of OR statements in brackets, so you end up with a query that asks for ANY of the conditions to be met (including id > 0 by itself)
  3. You have not prepended a wildcard character to the search string, meaning you will only find records that start with 'foo', and not records that contain 'foo' somewhere else.

The suggested changes above will fix these problems.

For whole specific word you need means you have to go through with double %% before and after the word

  if ($_REQUEST["keyword"]<>'') {
    $search_string = "AND trainer like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'  
OR venue like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR session like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR course like '%".mysql_real_escape_string($_REQUEST["keyword"])."%' 
OR category like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'";    
} 
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." 
WHERE id>0".$search_string;

}