duanpo7354
2019-02-05 18:01
浏览 927
已采纳

未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064查询功能

I am working on a framework and i'm calling the query function that should return me the query results .

This is the call

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

And i'm getting this error

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

And find all looks like

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

And this is the query function

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;
 }

I've checked the user id and it seems to be the good one : 7 I've checked the function and i don't saw any syntax error.

Code of find function :

 public function find($table, $params=[])
    {
       if($this->_read($table,$params))
      {
         return $this->results();
      }
       return false;
    }
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dpb75177 2019-02-05 19:26
    最佳回答

    As you are working with SQL you should use order by instead of order

    $contacts = $this->ContactsModel->findAllByUserId(Users::currentLoggedInUser()->id,['order by'=>'lname , fname']);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题