开发者

Search mySQL table for string and return the name of the field it is in

开发者 https://www.devze.com 2023-03-10 10:18 出处:网络
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?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号