Search mySQL table for string and return the name of the field it is in
I have a table with 3 columns
title | subtitle | body
I need to search all the rows in the table and return which columns contain the search term. How can I do this?
Ultimately the idea is a basic "find & Replace&q开发者_如何学运维uot; system.
Something like:
query("SELECT * FROM table WHERE MATCH(title,subtitle,body) AGAINST (?)",
$yourSearchTerm); //> assuming PDO
while($row = fetch_assoc($query)) {
foreach( $row as $k=>$v ) {
if (strpos($v,$yourSearchTerm)!==false)
echo 'The search word was found in the column: '.$k.'<br />';
}
}
If you don't have pdo
mysql_query("SELECT * FROM table WHERE MATCH(title,subtitle,body) AGAINST ('$yourSearchTerm')");
then use
mysql_fetch_assoc();
精彩评论