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:
OR
statementsAND
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:
AND
winds up right next to 'id>0', a space before AND
fixes that problem.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)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;
}