Nicekiwi
Nicekiwi

Reputation: 4807

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" system.

Upvotes: 0

Views: 130

Answers (1)

dynamic
dynamic

Reputation: 48111

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();

Upvotes: 1

Related Questions