2017-05-04 76 views
1

我使用PHP PDO包裝類未捕獲的異常 'PDOException',而使用PDO包裝類

我收到以下錯誤

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty' in C:\xampp\htdocs\mysite\include\SimplePDO.php:200 Stack trace: #0 C:\xampp\htdocs\mysite\include\SimplePDO.php(200): PDO->prepare('') #1 C:\xampp\htdocs\mysite\include\SimplePDO.php(244): SimplePDO->query(false, Array, true) #2 C:\xampp\htdocs\mysite\articles.php(57): SimplePDO->get_results(false) #3 {main} thrown in C:\xampp\htdocs\mysite\include\SimplePDO.php on line 200 

包裝類代碼:

public function query($query, $bindings = array(), $internal_call = false) 
{ 
    $this->counter++; 
    $this->c_query = $this->pdo->prepare($query); 
    if(empty($bindings)) 
    { 
     $this->c_query->execute(); 
    } 
    else 
    { 
     $this->c_query->execute((array)$bindings); 
    } 

    //Alternate the response based on class internal vs. direct call to "query()" 
    if($internal_call === true) 
    { 
     return $this->c_query; 
    } 
    elseif($this->c_query && $this->lastid()) 
    { 
     return $this->lastid(); 
    } 
    else 
    { 
     return false; 
    } 
} 
//end query() 


    public function get_results($query, $bindings = array()) 
{ 
    return $this->query($query, $bindings, true)->fetchAll(); 
} 
//end get_results() 

和查詢我我正在嘗試如下:

$q1 = $database1->query("SELECT * FROM sa_articleuploads_by_admin WHERE language=? ORDER BY article_upload_date AND article_subject ASC ", array('marathi')); 
$objTotalRS1 = $database1->get_results($q1); 
$counts = 1; 
$len = $database1->num_rows($q1); 

if ($len !=0){ 
    foreach ($objTotalRS1 as $data){ 
    ......////rest of my code 

我查了一下,裏面有語言欄目,我也檢查了輸入錯誤。但無法找到爲什麼會拋出錯誤。

我與PHP版本5.6.15)

你的幫助感謝

+0

將PDO包裝到另一個類中的常見錯誤。不要導致維護噩夢,你只會將bug添加到一個完美工作的軟件中。你公司僱傭的下一個PHP可能知道如何使用PDO,但他需要從頭開始學習這個包裝。 – e4c5

回答

0

調用

$objTotalRS1 = $database1->get_results($q1); 

簡單地調用查詢()方法,這是XAMPP本地服務器上嘗試這種期望$ q1是要執行的SQL語句。所以get_results也許應該這樣做

return $query->fetch(); 

這樣就完成了準備查詢(查詢方法),並獲取在get_results結果的想法。