我正在研究一个框架,正在调用查询函数,该查询函数应该向我返回查询结果。
这是电话
$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;
}
答案 0 :(得分:0)
在使用SQL时,应使用order by
而不是order
$contacts = $this->ContactsModel->findAllByUserId(Users::currentLoggedInUser()->id,['order by'=>'lname , fname']);