如何在代码点火器中获取选择查询的静态值


How to get a static value on a select query in codeigniter?

如何在编码点火器中获取静态值作为结果?

它适用于简单的PHP查询

$this->db->query("select 'mytype' as type from users where id='1'")->result();输出:类型 = 'mytype'

但是当我尝试使用codeigniter时,它给出错误说未知列。

$this->db->select("'mytype' as type");
$this->db->where('id','1');
$this->db->get('users')->result();

它给出错误:没有 mytype 列。

请在编码器点火器中为此提供帮助,以获取输出结果中的静态值。

在运行代码点火器查询之前尝试$this->db->_protect_identifiers = FALSE;。为了处理这类问题。

不要在字段名称中使用单引号。请改用 ' 字符。

"select `mytype` as type from users where id='1'"

如果你在 sql 中运行精简:SELECT 'abcdefgh' as item;

你会得到这个:

item
--------------
abcdefgh

您的代码点火器查询应为:

$this->db->select("mytype as type");
$this->db->where('id','1');
$this->db->get('users')->result();

mytype中删除单引号。

并删除额外的s表单结果()。