简体   繁体   English

Codeigniter上的SQL查询限制和偏移量错误

[英]SQL query limit and offset error on codeigniter

Error Number: 1064 错误号:1064

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''3'' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的“ 3”附近使用正确的语法

SELECT * FROM tbl_testimoni order by id DESC limit 3 offset '3' SELECT * FROM tbl_testimoni顺序为id DESC限制3偏移量'3'

Filename: D:\\wamp\\www\\obatrohanifinalcopy\\system\\database\\DB_driver.php 文件名:D:\\ wamp \\ www \\ obatrohanifinalcopy \\ system \\ database \\ DB_driver.php

Line Number: 330 行号:330

Hello, I'm having problem with my query syntax. 您好,我的查询语法有问题。 I think the problem is in limit and offset query. 我认为问题出在limitoffset查询中。 Because I have tried without limit and offset and my script can work properly. 因为我尝试了没有limitoffset ,所以我的脚本可以正常工作。 But the pagination function doesn't work according to my wish. 但是分页功能不符合我的意愿。

This is my controller: 这是我的控制器:

$url=$this->uri->segment(3,0);
        $this->load->library('pagination');
        $config['base_url'] = site_url().'/testimonial/all/';
        $config['total_rows'] = $this->db->get('tbl_testimoni')->num_rows();
        $config['per_page'] = 3;
        $this->pagination->initialize($config);
        $data['page']=$this->pagination->create_links();

        $sql = " SELECT * FROM tbl_testimoni order by id DESC limit 3 offset ? ";
        $binds = array($url);
        $query = $this->db->query($sql, $binds);
        $artikel = $query->result_array();
        $data['action'] = 'testimonial/all';
        $data['artikel'] = $artikel;
        $data['content'] = 'content/testimoni';
        $this->load->view('template/default', $data);

and in my view the pagination was called using this code: 在我看来,使用以下代码调用了分页:

<?php if(!empty($page)) echo $page; ?>

and my code doesn't work properly. 而且我的代码无法正常工作。

Note: I was using this controller and view in my previous website but there I was using PostgreSQL and it worked. 注意:我正在使用此控制器并在以前的网站中查看,但是在那里使用了PostgreSQL,它可以工作。

Try typecasting the $binds variable as in $this->db->query($sql, intval($binds)); 尝试像$this->db->query($sql, intval($binds));那样强制转换$binds变量$this->db->query($sql, intval($binds));

Also, by the documentation $this->uri->segment() may return boolean, so you might want to handle that as well. 另外,通过文档$this->uri->segment()可能返回布尔值,因此您可能也想处理该值。

The offset should be a number not string '3' 偏移量应为数字,而不是字符串“ 3”

when you pass the $binds in $this->db->query($sql, $binds); 当您在$this->db->query($sql, $binds);传递$ binds时$this->db->query($sql, $binds); the DB Driver think its a value and replace your ? DB Driver认为它具有价值并替换您的? with '3' where it should be OFFSET 3 带有“ 3”的位置应OFFSET 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM