2013-12-10 300 views
-4

我正在使用mysqli庫處理項目,並且我已經達到了需要在方法內創建SELECT查詢的程度,具體取決於發送的參數。動態查詢創建MySQL

我正在查找的行爲與Android的SQLite類似,您將列作爲參數傳遞,將值作爲下一個參數傳遞。

我知道我可以創建查詢字符串,如果參數發送的地方的列和值,通過迭代他們,然後手動串聯字符串到最後的查詢字符串,但我不知道是否有任何核心庫,讓你做到這一點或任何其他方式

+2

只需使用準備好的語句和參數化查詢:http://us3.php.net/pdo.prepared-statements。 –

回答

0

您應該使用PDO prepared statements

 //$param = array(":querycon1" => "querycon", ":querycon2" => "querycon2"); 
    // $condition = "abc=:querycon1 AND xyz=:querycon2"; 

    protected function setQuery($column,$condition,$param){ 

     $this->query = "SELECT $column FROM tablename WHERE $condition"; 

     $this->param = $param // 
    $this->getQuery($this->query, $this->param); // to a method that processes the query 
}