duannaoye0732 2015-08-09 22:26 采纳率: 100%
浏览 22
已采纳

MySqli查询与PHP数组中的任何值一样

I have a PHP array containing values and I want to query the database to find any results where the ItemName contains any of the values in the array.

$current_item = 'Big Fancy Red Car';

$words = explode(' ',$current_item); // Array of words to search

And the query:

SELECT ID FROM store_accessories WHERE ItemName LIKE '%$words%'

How do I select the ID of any items in the table whose ItemName contains any of the values in the $words array?

  • 写回答

2条回答 默认 最新

  • dongyinpan9284 2015-08-09 22:37
    关注

    You can do it like this:

    1. First explode the value into an array:

    $terms = explode(' ', $search_term);

    1. Loop the array to add the LIKE

    foreach ($terms as $term) { $term = mysql_real_escape_string($term); $likes[] = "field LIKE '%$term%'"; }

    1. Add to the query:

    $sql = "select * from table where"; $sql .= implode(' OR ', $likes);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料