繁体   English   中英

Codeigniter活动记录错误? 查询构建器添加了一行

[英]Codeigniter active record bug? Query builder added a line

我认为Codeigniter(版本3.1.2)活动记录有问题...

我的模型函数是:

function select_country($ID, $typec)
{
    $data = '';

    $this ->db -> select ('*');
    $this ->db -> where ('ID', $ID);
    $query = $this ->db -> get ('country');

    if ($query->num_rows()>0){

        foreach ($query->result() as $row){
            $data=$row->$typec;
        }
    }   

    return $data;

}

当我运行它时,出现此错误:

发生数据库错误

错误号:1064

您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册,以在第1行的'* FROM country WHERE ID ='6'附近使用正确的语法

SELECT `ads`, * FROM `country` WHERE `ID` = '6'

文件名:models / Country_model.php

行号:40

我注意到另外一行:“ SELECT ads ,* FROM country WHERE ID ='6'”,但我不知道它从哪里来。 没有“广告”列。

如果我使用"$query = $this ->db -> query ('SELECT * FROM country WHERE ID = '.$ID); " ,则没有错误。

可能是什么问题呢?

像这样尝试...

function select_country($ID, $typec)
{
    $data = '';

    $this ->db ->select ('*');
    $this->db->from('country');
    $this ->db ->where ('ID', $ID);
    $query = $this->db->get();

    if ($query->num_rows()>0){
    $row = $query->row();//only on row on result so no need to use `result()`
    $data = $row->$typec 
    }   

    return $data;

}

更简单的查询:

$query = $this->db->get_where('country', array('ID' => $ID));

我不知道您是否还在寻找答案,但是请确保在开始新的数据库查询后不要调用select_country()函数。

基本上像这样的东西可能会导致这个错误:

// Start new query using the Query-Builder 
$this->db->select('*')->from('something');

// Get some additional data
$countries = $this->your_model->select_countries($your_id, $some_type);

// Execute the query
$query = $this->db->where_in('country_id', $countries)->get();

如果您这样做,请尝试在启动新查询之前调用select_countries()函数:

// FIRST: Get some additional data
$countries = $this->your_model->select_countries($your_id, $some_type);

// Start new query using the Query-Builder 
$this->db->select('*')->from('something');

// Execute the query
$query = $this->db->where_in('country_id', $countries)->get();

暂无
暂无

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

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