0

I am creating a search box where single search box searches from entire database field and give results. here I have my code and query for doing that:

<form action="" method="post">  
Search: <input type="text" name="term" /><br />  
<input type="submit" value="Submit" />  
</form>  

****Case1: Where I have used Where and AND Clause****

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM employee WHERE employee_name AND employee_age LIKE '%".$term."%'"; 

$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
echo 'ID: ' .$row['id'];  
echo '<br /> Employee Name: ' .$row['employee_name'];  
echo '<br /> Age: '.$row['employee_age'];    
}  

}
?>

P.s- I wants a textbox to search from entire database field.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • 3
    `WHERE employee_name LIKE 'something' AND employee_age LIKE 'something'` Please, read manuals before writing a question. – u_mulder Jul 14 '16 at 12:48
  • @u_mulder - 'something' could be one of the columns. so you really need `OR` instead of `AND`. also don't forget `CAST` to string before checking int or other non string columns. – ughai Jul 14 '16 at 12:50
  • your question is not clear. do you want a query to search from all the columns in your db table? do you want a query to search from only two columns? 1. mysql is deprecated 2. mysqli is the new improved version or use PDO instead Why so? a good link for reference i found right here in stack: http://stackoverflow.com/questions/2190737/what-is-the-difference-between-mysql-mysqli-and-pdo – coolstoner Jul 14 '16 at 12:53
  • I want to search from all columns in db table –  Jul 14 '16 at 13:04

1 Answers1

0

I can use OR Condition, if I wants to search in all db field.

here is the condition:

$sql = "SELECT * FROM employee WHERE employee_name  LIKE '%".$term."%' or employee_age LIKE '%".$term."%' "; 

(Suppose there are only two fields in db named employee_name, employee_age)