php - 如何在 php mysql 查询中使用数组?

标签 php mysql where-in

我一直在尝试使用 $keyword 中的 where site_keywords 从数据库中检索所有 site_keywords。但它没有显示任何错误或输出。

$user_query = $_REQUEST['user_query'];
$search=preg_split('/\s+/',$user_query);
$keywords = join(",",$search); 
$query = "select * from sites where site_keywords in ('%$keywords%') order by rank DESC ";

有人可以帮我解决这个问题吗?

最佳答案

连接(内爆)函数中缺少一些单引号:

$user_query = $_REQUEST['user_query'];
$search=preg_split('/\s+/',$user_query);
$keywords = join("','",$search); 
$query = "select * from sites where site_keywords in ('%$keywords%') order by rank DESC ";

不带这些引号的查询:

...where site_keywords in ('one,two,three')...

这不会产生任何输出或错误,因为没有有效结果。搜索查询被视为一个长字符串。

使用这些报价进行查询:

...where site_keywords in ('one','two','three')...

这里每个查询都正确地拆分为多个搜索值。

关于php - 如何在 php mysql 查询中使用数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44969901/

相关文章:

MySQL - INSERT INTO table SELECT WHERE field IN(子查询) - 使用 IN 使事情变得非常慢,正确的方法是吗?

c# - 将 C# 字符串作为 SELECT WHERE IN 的 SQL 参数传递

javascript - 节点 MySQL 插入顺序

php - 时间相加减去时间

javascript - PHP 使用 AJAX 将数据 POST 到另一个页面

php - Laravel - 迁移时找不到驱动程序

Mysql 索引键名重要性

mysql - 在 SQL 中有效地加入间隔范围

sql - 使用 ODBC 的 Dapper WHERE IN 语句

php - __set/__get 数组属性