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

未捕获的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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!