2017-08-10 65 views
0
public function search_between() { 

$min = $this->request->query['min']; 
$max = $this->request->query['max']; 

$conditions = array('Ad.price BETWEEN ? AND ?' => array($min,$max)); 
$ads = $this->Anuncio->find('all',array('conditions' => $conditions)); 
$this->set('ads',$ads); 

} 

警告:strlen()期望參數1是字符串,數組給定。CakePHP 2.8爲什麼我的search_between函數給我一個警告?

**Thank's for your help I'm new to asking questions here.** 
+0

你能張貼你在哪裏調用'strlen的()' ? – bill

+0

我不調用strlen()它的一個警告顯示cakephp當我將數組中的變量($ min,$ max)傳遞給Between:array('Ad.price BETWEEN?AND?'=> array( $ min,$ max)< - 這些數組會導致警告,但我不知道爲什麼。謝謝 –

+0

你可以嘗試拋出你的請求生成的sql嗎?這是一個很好的方法來開始調試這個 – Amjo

回答

0

試試這個:

public function search_between() { 

$min = $this->request->query['min']; 
$max = $this->request->query['max']; 

$conditions = array('Anuncio.price >=' => $min,'Anuncio.price <=' $max)); 
$ads = $this->Anuncio->find('all',array('conditions' => $conditions)); 
$this->set('ads',$ads); 
+0

我試過這些代碼。工作我糾正你的代碼中的一些錯誤,並嘗試但不工作:'Anuncio.price <='<---和這些---->'Anuncio.price> =')顯示錯誤錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法出錯,無法正常工作。謝謝您的幫助。 –

0

您可以使用CakePHP MySQL查詢將作品等作爲CakePHP的查詢

if(!empty($min) && !empty($max)){ 
    $ads = $this->Anuncio->query('SELECT * from Anuncio WHERE Anuncio.price BETWEEN "'.$min.'" AND "'.$max.'"'); 
} 

else{ 
$ads = $this->Anuncio->query('SELECT * from Anuncio'); 
} 
+0

我要試一試你的代碼如果它有效,我會結束這個問題。非常感謝你! –

+0

我在網上讀到strlen()不支持數組,如果你傳遞一個數組,警告顯示。 –

+0

我修復了在位於c:\ wamp \ your_project_name \\ lib \ Cake \ Utility \ CakeText.php第221行的文件中更改strlen的問題,並更改了$ offset = $ pos + strlen($ val);到$ offset = $ pos + count($ val);只能改變strlen來計數,現在警告會按預期飛走代碼工作。 Tnx的幫助。 –

相關問題