0

这是我想做的,但似乎不可能:(编辑:将单引号更改为双引号)

function get_archives($limit, $offset) 
{
    $query = $this->db->query("
        SELECT  archivalie.id, 
                archivalie.signature, 
                type_of_source.description AS type_of_source_description, 
                media_type.description AS media_type_description,
                origin.description AS origin_description

        FROM    archivalie, 
                type_of_source, 
                media_type,
                origin

        WHERE   archivalie.type_of_source_id = type_of_source.id                                                        
        AND     type_of_source.media_type_id = media_type.id  
        AND     archivalie.origin_id = origin.id                                                                     

        ORDER BY    archivalie.id ASC
        LIMIT       $limit, $offset
    "); 


    // etc...

}

它给出了这个错误:(编辑:使用双引号的新错误消息,并在 URL 中传递了一个偏移量)

ERROR: LIMIT #,# syntax is not supported HINT: Use separate LIMIT and OFFSET clauses.

它仅在您使用 ActiveRecord 格式传递变量时才有效:

$this->db->select('archivalie.id, archivalie.signature, etc, etc');
// from, where, etc.
$this->db->limit($limit, $offset);        
$query = $this->db->get();
4

4 回答 4

2
$query = $this->db->query('
SELECT  archivalie.id, 
        archivalie.signature, 
        type_of_source.description AS type_of_source_description, 
        media_type.description AS media_type_description,
        origin.description AS origin_description
FROM    archivalie, 
        type_of_source, 
        media_type,
        origin
WHERE   archivalie.type_of_source_id = type_of_source.id                                                        
AND     type_of_source.media_type_id = media_type.id  
AND     archivalie.origin_id = origin.id                                                                     
ORDER BY    archivalie.id ASC
LIMIT       ?
OFFSET      ?',array($limit,$offset));
于 2012-10-14T16:14:03.477 回答
1

这有效:

$query = $this->db->query("
    SELECT  archivalie.id, 
            archivalie.signature, 
            type_of_source.description AS type_of_source_description, 
            media_type.description AS media_type_description,
            origin.description AS origin_description

    FROM    archivalie, 
            type_of_source, 
            media_type,
            origin

    WHERE   archivalie.type_of_source_id = type_of_source.id                                                        
    AND     type_of_source.media_type_id = media_type.id  
    AND     archivalie.origin_id = origin.id                                                                     

    ORDER BY    archivalie.id ASC
    LIMIT       $limit
    OFFSET      $offset
");

但如果 URL 中不存在偏移量,则需要检查以分配默认值。从我的控制器:

# Check/assign an offset
$offset = (!$this->uri->segment(3)) ? 0 : $this->uri->segment(3);

# Get the data
$archives = $this->archive->get_archives($config['per_page'], $offset);
于 2008-10-10T08:41:36.557 回答
0

它与参数的数据类型有关。如果您不希望 Codeigniter 添加 `'' 单引号,请确保您传递的是整数。

$page = '10'; // for some reason this arrives as a string
$sql = "select * from customers limit 10 offset ?";
$result = $this->db->query($sql, array(intval($page));
于 2016-12-07T02:28:38.083 回答
-1

如果你使用双引号而不是单引号,它会起作用,但如果变量没有被正确清理,你就会受到注入攻击。

于 2008-10-09T15:49:42.603 回答