简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM