簡體   English   中英

未捕獲的PDOException:SQLSTATE [42000]:語法錯誤或訪問沖突:1064查詢函數

[英]Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 query function

我正在開發一個框架,正在調用應該返回查詢結果的查詢函數。

這是電話

$contacts = $this->ContactsModel->findAllByUserId(Users::currentLoggedInUser()->id,['order'=>'lname, fname']);

我得到這個錯誤

Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have
 an error in your SQL syntax; check the manual that corresponds to your MariaDB server 
 version for the right syntax to use near 'BY lname, fname' at line 1 in 
  E:\XAMP\htdocs\framework\core\DB.php:34 Stack trace: #0 
  E:\XAMP\htdocs\framework\core\DB.php(34): PDO->prepare('SELECT * FROM c...') #1 
  E:\XAMP\htdocs\framework\core\DB.php(105): DB->query('SELECT * FROM c...', Array) #2 
  E:\XAMP\htdocs\framework\core\DB.php(119): DB->_read('contacts', Array) #3 
  E:\XAMP\htdocs\framework\core\Model.php(63): DB->find('contacts', Array) #4 
  E:\XAMP\htdocs\framework\app\models\Contacts.php(20): Model->find(Array) #5 
  E:\XAMP\htdocs\framework\app\controllers\ContactsController.php(16): Contacts->findAllByUserId(7, Array) #6 E:\XAMP\htdocs\framework\core\Router.php(32):
 ContactsController->indexAction() #7 E:\XAMP\htdocs\framework\index.php(35): 
Router::route(Array) #8 {main} thrown in E:\XAMP\htdocs\framework\core\DB.php on line 34

並找到所有看起來像

  public function findAllByUserId($user_id,$params=[])
        {
          $conditions = [
            'condition' => 'user_id = ?',
            'bind' => [$user_id]
          ];
          $conditions = array_merge($conditions, $params);
          return $this->find($conditions);
        }

這就是查詢功能

public function query($sql, $params = [], $class=false)
  {
   $this->_error = false;
   if($this->_query = $this->_pdo->prepare($sql))
   {
     $x = 1;
     if(count($params)) {
       foreach($params as $param) {
         $this->_query->bindValue($x, $param);
         $x++;
       }
     }
     if($this->_query->execute())
     {
       if($class){
         $this->_result = $this->_query->fetchAll(PDO::FETCH_CLASS,$class);
       } else {
         $this->_result = $this->_query->fetchALL(PDO::FETCH_OBJ);
       }
       $this->_count = $this->_query->rowCount();
       $this->_lastInsertID = $this->_pdo->lastInsertId();
     } else {
       $this->_error = true;
     }
   }
   return $this;
 }

我已經檢查了用戶ID,它似乎是一個不錯的選擇: 7我已經檢查了該函數,但沒有看到任何語法錯誤。

查找功能代碼:

 public function find($table, $params=[])
    {
       if($this->_read($table,$params))
      {
         return $this->results();
      }
       return false;
    }

使用SQL時,應使用order by而不是order

$contacts = $this->ContactsModel->findAllByUserId(Users::currentLoggedInUser()->id,['order by'=>'lname , fname']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM