2016-03-11 32 views
-1

我遇到MySQL的PHP​​語法問題。當在sql查詢中使用Like時,不會返回任何結果

我的代碼:

$q['item_desc LIKE \'%?%\'"'] = array($criteria); 

但它被翻譯成SQL查詢:

SELECT `Item`.`id`, `Item`.`item_desc`, `Item`.`row_num`, `Item`.`shelf_num`, `Item`.`shelf_loc`, `Item`.`datetime` FROM `inventory`.`items` AS `Item` WHERE `item_desc` LIKE '%'10'%'" 

我相信我需要擺脫單引號的%內。我該怎麼做呢?

+0

爲什麼有雙引號在那裏?嘗試'$ q ['item_desc LIKE \'%?%\'']'或'$ q [「item_desc LIKE'%?%'」]' – Gavin

+0

請添加您的實際代碼。你在使用一些庫或框架嗎? PHP的mysql函數不會翻譯SQL。 – ryantxr

+0

是的,我正在使用CakePHP框架。以下是搜索方法的代碼。公共職能搜索(){ $ criteria = $ this-> request-> query ['criteria']; $ this-> request-> data ['criteria'] = $ criteria; $ q [「item_desc LIKE'%?%'」] = array($ criteria); $ results = $ this-> Item-> find('all',array( 'conditions'=> array( $ q ) )); $ this-> set(compact('results')); } – lethalr80

回答

0

那裏有一個不需要的雙引號。

嘗試

$q['item_desc LIKE \'%?%\''] 

$q["item_desc LIKE '%?%'"] 
相關問題