2
votes

I think I have a problem with codeigniter (version 3.1.2) active record...

My model function is:

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;

}

When I run it I get this error:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM country WHERE ID = '6'' at line 1

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

Filename: models/Country_model.php

Line Number: 40

I noticed an additional line : "SELECT ads, * FROM country WHERE ID = '6'" but I don't know from where is it. There is no 'ads' column.

If I use "$query = $this ->db -> query ('SELECT * FROM country WHERE ID = '.$ID); " there is no error.

What could be the problem?

2

2 Answers

0
votes

Try like this....

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;

}

More simpler query:

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

I don't know if you're still looking for an answer but make sure you don't call the select_country()-function after starting a new DB-query.

Basically something like this MAY cause this bug:

// 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();

If you do something like this, try calling your select_countries()-function before starting the new query:

// 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();