在php中使用mysql like子句

时间:2014-04-25 06:29:42

标签: php mysql sql mysqli

对我来说很好的Mysql查询是:

SELECT *
FROM `review_details`
WHERE category = 'italian'
AND review LIKE '%cozy%';

显示结果。

SELECT *  FROM `review_details`    WHERE category = 'italian'    AND review LIKE %cozy%';

但是当我在我的php脚本中执行时:

$word = 'cozy';
$select = mysqli($con,"SELECT *  FROM `review_details`    WHERE category = 'italian'    AND review LIKE %.$word.%' ");

然后它不会显示任何结果。

请查看查询是否正确,当我更改条件时会给出结果。

5 个答案:

答案 0 :(得分:3)

LIKE %.$word.%'更改为LIKE '%$word%'

答案 1 :(得分:0)

将其改写为..

$select = mysqli_query($con,"SELECT * FROM `review_details` WHERE category = 'italian' AND review LIKE '%$word%' ");

连接根本不是必需的。

应该是mysqli_query。由Husseyin.

指出

答案 2 :(得分:0)

您的语法错误,您需要使用mysqli_query$mysqli->query(PDO),并在sql语句中引用一些问题。您可以使用以下内容;

$word = 'cozy';
$select = mysqli_query($con,"SELECT *  FROM `review_details`    
                WHERE `category` = 'italian'    AND `review` LIKE '%$word%' ");

或者,如果您使用的是pdo,则可以使用;

// $mysqli is the conn object
$word = 'cozy';
$select = $mysqli->query("SELECT *  FROM `review_details`    
                WHERE `category` = 'italian'    AND `review` LIKE '%$word%' ");

答案 3 :(得分:-1)

试试这个

$word = 'cozy';
$select = mysqli($con,"SELECT *  FROM `review_details`    
                WHERE `category` = 'italian'    AND `review` LIKE '%$word%' ");

答案 4 :(得分:-1)

试试这个:

$word = 'cozy';
$select = mysqli($con,"SELECT *  FROM `review_details`    WHERE category = 'italian'    AND review LIKE '%{$word}%' ");

但我建议使用mysqli面向对象的样式而不是程序样式。然后使用$con> prepare()。