我想使用Doctrine做一個準備好的SQL語句。 如果我硬編碼在SQL查詢它的工作的價值:爲什麼我得到一個PDOException:SQLSTATE [42000]
public function search()
{
$sql = 'SELECT `name` FROM `files` WHERE `name` = "myfile.exe"';
$query = $this->conn->prepare($sql);
$status = $query->execute();
$searchResult = $query->fetchAll();
$searchResult = array(
'service' => $searchResult,
'status' => $status
);
$searchResult = $this->conn->fetchAll($sql);
return $searchResult;
}
但是,如果我試圖用一個參數傳遞值不中,給我的PDOException:SQLSTATE [42000]
public function search()
{
$sql = 'SELECT `name` FROM `files` WHERE `name` = :service';
$query = $this->conn->prepare($sql);
$query->bindValue(":service", "myfile.exe");
$status = $query->execute();
$searchResult = $query->fetchAll();
$searchResult = array(
'service' => $searchResult,
'status' => $status
);
$searchResult = $this->conn->fetchAll($sql);
return $searchResult;
}
我無法弄清楚我做錯了什麼。在文檔http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
您是否嘗試過直接從MySQL命令行運行sql語句來查看是否有效?此外,這是純粹的學說代碼,還是使用Laravel或Symfony之類的PHP框架? –