【问题标题】:Passing 2 strings through array to the where clause in codeigniter通过数组将2个字符串传递给codeigniter中的where子句
【发布时间】:2013-07-27 23:26:52
【问题描述】:

我想通过数组从控制器发送 2 个字符串到模型并从 db 中获取结果,但是我遇到了一个问题。

我的控制器是这样的:

$data = array();
if($query = $this->authors_model->get_authors_list(array('author_Type' => array('admin', 'author'))))
{
    $data['authors'] = $query;
}

我的模特:

function get_authors_list($options = array())
{        

    if(isset($options['author_Type']))
    $this->db->where('author_Type', $options['author_Type']);

    $this->db->order_by('author_Id', 'ASC');
    $query = $this->db->get('mg_authors');

    return $query->result();
}

以及我得到的错误:

遇到了 PHP 错误

严重性:通知

消息:数组到字符串的转换

文件名:数据库/DB_active_rec.php

行号:427


错误号:1054

“where 子句”中的未知列“数组”

SELECT * FROM (mg_authors) WHERE author_Type = 数组 ORDER BY author_Id ASC 限制 15

文件名:D:\xampp\htdocs\sport\system\database\DB_driver.php

行号:330

【问题讨论】:

  • 您将$options['author_Type'] 数组传递给AR::where() 方法,该方法生成author_Type = Array。这是一个错误。

标签: arrays codeigniter where-clause


【解决方案1】:

放置数组时需要使用 WHERE IN。在 CodeIgniter 中,它需要这样做:

$this->db->where_in('author_Type', $options['author_Type']);

【讨论】:

    猜你喜欢
    • 2012-01-13
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2012-10-23
    相关资源
    最近更新 更多