简体   繁体   English

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

[英]how to use an array in php mysql query?

I've been trying to retrieve all site_keywords from the database, using where site_keywords in $keyword. 我一直在尝试使用$ keyword中的site_keywords从数据库中检索所有site_keywords。 But it doesn't show any error or output. 但是它没有显示任何错误或输出。

$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 ";

Can anyone help me with this? 谁能帮我这个?

There are some missing single quotes in the join (implode) function: 连接(内插)函数中缺少一些单引号:

$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 ";

Query Without these quotes: 没有这些引号的查询:

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

This will not produce any output or error as there are no valid results. 由于没有有效的结果,因此不会产生任何输出或错误。 The search query is treated as one long string. 搜索查询被视为一个长字符串。

Query With these quotes: 用这些引号查询:

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

Here each query is correctly split in multiple search values. 在这里,每个查询正确地分为多个搜索值。

$query = "select * from sites where site_keywords in (".implode(",",$keywords).") order by rank DESC ";

IN does a literal search, to do a "fuzzy" search you need to do something like: IN进行文字搜索,要进行“模糊”搜索,您需要执行以下操作:

$query = "SELECT * FROM sites WHERE ".implode(" OR ", array_fill(0,count($search),"site_keywords LIKE ?"); 
 //Query looks like SELECT * FROM sites WHERE site_keywords LIKE ? OR site_keywords LIKE ?

$search = array_map(function ($v) { 
    return "%$v%";
},$search); 

Now for the binding, it depends what you're using: 现在进行绑定,这取决于您使用的内容:

//MySQLi 
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, array_fill(0,count($search),"s"), ...$search); //Note, you may bet some issues with references here. 
mysqli_stmt_execute($stmt);

//PDO
$stmt = $connection->prepare($query); 
for ($i = 0;$i< $search;$i++) {
    $stmt->bindValue($i+1,$search[$i]);
} 
$stmt->execute();

Always use prepared statements to prevent SQL injection. 始终使用准备好的语句来防止SQL注入。 The following code can be used as a starting point to solve your problem (needs the PDO library, http://php.net/manual/en/book.pdo.php ). 以下代码可以用作解决您的问题的起点(需要PDO库http://php.net/manual/en/book.pdo.php )。

$user_query = $_REQUEST['user_query'];                      // you should better use $_GET or $_POST explicitly
$user_query = preg_replace('#\s{2,}#', ' ', $user_query);   // replace multiple spaces with a single space
$keywords = explode(' ', $user_query);                      // create the keywords array
$placeholders = array_fill(0, count($keywords), '?');       // create the placeholders array

$sql = 'SELECT *
        FROM sites
        WHERE site_keywords IN (' . implode(', ', $placeholders) . ')
        ORDER BY rank DESC';

$stmt = $db->prepare($sql);
$stmt->execute($keywords);
$result = $stmt->fetchAll();

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

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