布尔模式,其中使用PHP MySQLi预准备语句对动态查询值进行匹配查询 [英] Boolean Mode Where Match Query with Dynamic Against Values, using PHP MySQLi Prepared Statements

查看:47
本文介绍了布尔模式,其中使用PHP MySQLi预准备语句对动态查询值进行匹配查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mysqli prepared statements使用Where Match查询来查询mysql.问题是Boolean Mode AGAINST values

I want to query mysql with a Where Match query using mysqli prepared statements. The problem is the Boolean Mode AGAINST values,

通常:(+value1 +value2 +value IN BOOLEAN MODE)

但是问题是我不能在准备好的语句中使用它,例如(? ? ? IN BOOLEAN MODE)因为值的数量会有所不同,所以它不是固定的.

but the problem is that I can't use it in prepared statements e.g. (? ? ? IN BOOLEAN MODE) because the number of values will differ, its not fixed.

我尝试了以下代码:

$keywords = explode(" ", $SearchResults->squery);   
foreach($keywords as $key=>$value)
{
    $keywords[$key] = '+'.$value;
}
$keywords = implode(",", $keywords);
$dbconnect = new mysqli($host, $user, $pw, $db);
$stmt = $dbconnect->prepare("SELECT `postid` FROM `espina5_jotecodb`.`posttd` WHERE MATCH (`title`) AGAINST (? IN BOOLEAN MODE)"); 
$stmt->bind_param('s', $keywords)
if($stmt->execute())
{
    $stmt->bind_result($col1);
    while ($stmt->fetch()) 
    {
        echo $col1."<br>";
    }
}
$dbconnect->close();

bind_param期间的

$keywords将只有一个包含+value1 +value2 +value3

$keywords during bind_param will have a single string containing +value1 +value2 +value3

上面的代码的问题是它不是单独读取值而是作为一个整体读取,我认为这是因为准备语句时使用了单个?以及绑定期间使用了单个s.我以为我可以那样做,我猜我错了.

The problem with the above code is that its not reading the values individually but as a whole, i think its because of the single ? when the statement was prepared and single s during bind. I assumed I could do it like that, guess I was wrong.

因此,无论如何,上述代码将导致读取第一个值,而忽略后续值,因为该值被视为单个值.因此,我想问如何在布尔模式下实现具有动态值的预准备语句查询?我应该使用每个值分别查询数据库吗?

So anyway the above code will result in reading the first value and disregard the succeeding values because the value is treated as a single value. So I am asking how can I achieve prepared statement queries with dynamic values for the boolean mode? Should I resort to querying the database individually per value?

推荐答案

您这样做的方式是错误的.

You are doing it just wrong way.

通常:(+ value1 + value2 + value在BOOLEAN模式下)

normally: (+value1 +value2 +value IN BOOLEAN MODE)

不是. 通常必须是

('+value1 +value2 +value' IN BOOLEAN MODE)

用引号引起来,不带逗号

使用占位符也是如此:您必须在PHP中组装搜索字符串,然后通过单个占位符将其完全绑定.

the same goes with using placeholders: you have to assemble your search string in PHP, and then bind it whole via single placeholder.

这篇关于布尔模式,其中使用PHP MySQLi预准备语句对动态查询值进行匹配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆