user123
user123

Reputation: 5407

Using mysql like clause in php

Mysql query that works fine for me is :

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

It display results.

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

But when I execute in my php script :

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

Then it does not display any result.

Please see query is correct, when I change the condition it gives result.

Upvotes: 1

Views: 113

Answers (5)

train_fox
train_fox

Reputation: 1537

Try this:

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

But i suggest to use mysqli object oriented style instead of procedural style. Then use $con->prepare().

Upvotes: -1

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

Your syntax is wrong, you need to use mysqli_query or $mysqli->query (PDO) , and some quote problem in sql sentence. You can use following;

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

Or if you are using pdo, you can use;

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

Upvotes: 0

Rewrite it as..

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

The concatenation is not at all neccessary.

It should be mysqli_query. Pointed out by Husseyin.

Upvotes: 0

MinhD
MinhD

Reputation: 1810

Change LIKE %.$word.%' into LIKE '%$word%'

Upvotes: 3

Sadikhasan
Sadikhasan

Reputation: 18600

Try this

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

Upvotes: -1

Related Questions