简体   繁体   English

在 mySQL 表中搜索字符串并返回其所在字段的名称

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

I have a table with 3 columns我有一个有 3 列的表

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如果没有 pdo

  mysql_query("SELECT * FROM table WHERE MATCH(title,subtitle,body) AGAINST ('$yourSearchTerm')");

then use然后使用

   mysql_fetch_assoc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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