0

我想执行这个 mysql 搜索:

 SELECT ida, MotsClef FROM Actes WHERE MATCH (MotsClef ) 
 AGAINST ('+"dette" +"plège"' IN BOOLEAN MODE);

使用 php,我使用正则表达式将 +" 和 " 添加到通过 $_POST 接收的表达式中,因此 var_dump 给出:

'motcle' => string '+"dette" +"plège"'

所以这也很好。但是,我使用 PDO 类使用准备好的语句,并且我有这段代码:

 if($r['motcle']!=''){
     $motclef = $r['motcle'];
     $demMotsClef = " AND WHERE MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";
    }
    else{
            $demMotsClef='';
    }

比:

 $f = "SELECT COUNT(*) FROM Actes, Bibliographie WHERE id = idBiblio".$demMotsClef;

$demande = $this->prepare($f);

if($r['motcle']!=''){$demande->bindValue(':motsclef',stripslashes($motclef));}

$demande->execute(); //the error is on this line//

我收到一条 MySQL 错误消息,说我的 SQL 语法有错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:
Syntax error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the 
right syntax to use near 'WHERE MATCH (MotsClef ) AGAINST 
('+\"dette\" +\"plège\"' IN BOOLEAN MODE) AND a' at line 1' in 
/Library/WebServer/Documents/messources/actions.class.php on line 547.

mysql语法中的错误是添加了斜杠,因此使用了stripslashes(不起作用)。

关于如何解决这个问题的任何想法 - 我宁愿不要在 php.ini 或 .php 函数中更改 ini 设置,因为这会弄乱我所有其他 mysql 请求。

谢谢!

4

1 回答 1

1

哦,我花了一段时间才找到错误,但这绝对是错误的:

$demMotsClef = " AND WHERE MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";

$f = "SELECT COUNT(*) FROM Actes, Bibliographie WHERE id = idBiblio".$demMotsClef;

如果你看这个,你会有双 WHERE,这是不允许的,你应该做这个改变:

$demMotsClef = " AND MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";
于 2015-07-06T06:36:17.433 回答