在PHP中选择MySQL的麻烦

时间:2012-10-08 16:42:31

标签: php mysql

我收到了这个错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   '和idcat = 00或idcat = 11或idcat = 4或idcat = 8或idcat附近   在第6行= 10或idcat'

我尝试在PHP上执行此查询

SELECT *,
        MATCH (name,description) AGAINST ('key') AS relevancia
  FROM offer
  WHERE
      MATCH (name,description)
          and idcat = 00
          OR idcat = 11
          OR idcat = 4
          OR idcat = 8
          OR idcat = 10
          OR idcat = 5
          OR idcat = 7
          OR idcat = 1
          OR idcat = 2
          OR idcat = 3
          OR idcat = 6
          OR idcat = 9
      AGAINST ('key')
  ORDER BY discount DESC
  LIMIT 0, 15

但是,如果我尝试直接在phpmyadmin上执行此查询,它可以正常工作。所以我不知道为什么不能在PHP上工作,如果它是相同的咨询。

请原谅我可怜的英语。

修改

这是php consulation

$sql2 = "SELECT *, 
MATCH (name,description) 
AGAINST ('$search')
AS relevancia 
FROM offer 
WHERE MATCH (name,description) AGAINST ('$search')  
and  $wheres $order $limit";

echo $sql2;
mysql_query("".$sql2."")  or die (mysql_error());

$ sql2上的php结果

SELECT *, MATCH (name,description) AGAINST ('key') AS relevancia 
FROM offer 
WHERE MATCH (name,description) AGAINST ('key') 
and idcat IN(00, 11, 4, 8, 10, 5, 7, 1, 2, 3, 6, 9) 
ORDER BY discount DESC LIMIT 0, 3

php-mysql中的错误

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
'idcat IN(00, 11, 4, 8, 10, 5, 7, 1, 2, 3, 6, 9) ORDER BY discount DESC' at line 5

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT *, concat (name, relevance) AS relevancia 
FROM offer 
WHERE MATCH (name,description) AGAINST  ('key') 
and (idcat = '00' 
OR (idcat > 0 
and idcat<=11))
ORDER BY discount DESC LIMIT 0, 15

答案 1 :(得分:1)

您不得将MATCH (col1,col2,...) AGAINST (expr [search_modifier])条款与idcat上的条件分开。

先完成一个,然后将它与另一个相结合。也就是说:

... WHERE MATCH (name,description) AGAINST ('key') AND idcat IN ( 00, 11, ... )

或者:

... WHERE idcat IN ( 00, 11, ... ) AND MATCH (name,description) AGAINST ('key')

注意:我使用MySQL的IN运算符作为(idcat = 00 OR idcat = 11 OR ...)的简写。