codeigniter绑定的选择查询 [英] Codeigniter select query with bindings

查看:192
本文介绍了codeigniter绑定的选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的问题,我解决不了。在codeigniter我创建了一个模型,谁做到了这一点:

I have a big problem, what i can't solve. In codeigniter i created a model, who done this:

public function listazas($mettol, $mennyit, $feltetel)
    {
        $query = "SELECT * FROM vicc ORDER BY ? DESC LIMIT ?,?";
        $query = $this->db->query($query, array($feltetel, $mettol, $mennyit));

        return $query->result_array();
    }

在控制器我使用它:

   $viccek = $this->index_model->listazas(0, 10, "ertekeles");

   $this->load->view('index/index', array(
       'viccek' => $viccek
   ));

和这里的sql不要做部分的顺序......为什么?

And here the sql don't do the order by section... why?

推荐答案

好,那是因为你正在做一个 ORDER BY'列'而不是 ORDER BY列

Well it's because you're doing a ORDER BY 'column' instead of ORDER BY column.

您将不得不做一个对当前功能替换:

You'll have to do a replace on current function with:

public function listazas($mettol, $mennyit, $feltetel)
{
    $feltetel = $this->db->escape_like_str($feltetel);
    $query = "SELECT * FROM vicc ORDER BY {$feltetel} DESC LIMIT ?,?";
    $query = $this->db->query($query, array($mettol, $mennyit));

    return $query->result_array();
}

Basicly你的查询()躲过了 $ feltetel 它周围,使它像一个字符串而不是列名。

Basicly your query() escaped the $feltetel with '' around it, making it act like a string instead of a column name.

有关这一个简单的查询,你可以很容易通过活动记录的。

For a query as simple as this one, you can do it easier through Active Records.

您也可以尝试通过运行一个回声$这个 - &gt将这个自己解决; DB-> last_query(); $这 - > DB-GT&;查询()和比较的结果结果。
然后,你就已经注意到了 ORDER BY

You could also try to troubleshoot this yourself by running a echo $this->db->last_query(); after the $this->db->query() and compare the result.
Then you would had noticed the '' after ORDER BY.

这篇关于codeigniter绑定的选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆