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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器