2015-05-12 33 views
1

這裏是下面的查詢,我得到正確的方法來傳遞%param與%一樣嗎?

public function getSearchResult($searchString){ 
     $stmt = $this->conn->prepare("SELECT t.* FROM company t WHERE t.company_name like ? "); 
     $stmt->bind_param("i", "%".$searchString."%"); 
     $stmt->execute(); 
     $tasks = $stmt->get_result(); 
     $stmt->close(); 
     return $tasks; 
    } 

錯誤是

Fatal error: Cannot pass parameter 2 by reference in

我想我做的不正確之類參數(因爲它適用於其中=條件)。我很苗條,還有什麼新的幫助?

+1

你可以通過它在執行像一個數組。 – chris85

回答

3

你必須形成串,然後才能在bind_param()

public function getSearchResult($searchString){ 
    $search = '%'. $searchString . '%'; 
    $stmt = $this->conn->prepare("SELECT t.* FROM company t WHERE t.company_name like ? "); 
    $stmt->bind_param("s", $search); 
    $stmt->execute(); 
    $tasks = $stmt->get_result(); 
    $stmt->close(); 
    return $tasks; 
} 

使用它除了它看起來像你傳遞一個字符串,而不是一個整數,請確保您的類型設置爲"s"

2

你需要設置你的i變量的位置。

$stmt = $this->conn->prepare("SELECT t.* FROM company t WHERE t.company_name LIKE :i "); 
$stmt->bind_param(":i", "%$searchString%", PDO::PARAM_STR); 

Here is an example in php.net

2
<?php 
public function getSearchResult($searchString){ 
     $stmt = $this->conn->prepare("SELECT t.* FROM company t WHERE t.company_name like ? "); 
     $stmt->bind_param("s", "%$searchString%",PDO::PARAM_STR); 
     $stmt->execute(); 
     $tasks = $stmt->get_result(); 
     $stmt->close(); 
     return $tasks; 
    } 

搜索字符串在條件所以這將是s並添加PDO::PARAM_STR

相關問題