簡體   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