h_a86
h_a86

Reputation: 281

codeigniter SELECT query function

I am new to codeigniter and trying to access data from mysql database

here is my model class code

function model_data($a){
$this->load->database();
$query = $this->db->query("SELECT* FROM mytable3");
return $query->result();    
}

function model_data_cat(){
$this->load->database();
$query = $this->db->query("SELECT* FROM mytable1");
return $query->result();    
} 

Actually i am calling two functions in my controller. one is model_data and the other is model_data_cat. but i get error "Parse error: syntax error, unexpected $end in "

However when i try this piece of code it works perfectly.

function model_data($a){
$this->load->database();
$query = $this->db->get('mytable3');    
return $query->result();    
}

function model_data_cat(){
$this->load->database();
$query = $this->db->get('mytable3');    
return $query->result();    
} 

Can anybody helps me... Thanks.

Upvotes: 0

Views: 6267

Answers (3)

Deepak Sharma
Deepak Sharma

Reputation: 11

You can call the library in autoloads in config folder

The query can be generated in different ways like:

$res = $this->db->get('table_name')->result();
return $res;

will be accessible with foreach loop like

foreach($res as $key){ $value = $res->result();

}

Upvotes: 1

MikeCruz13
MikeCruz13

Reputation: 1254

Unexpected $end almost always means you missed an ending brace, parenthesis, quote, etc. somewhere in the code and it's not in that spot.

Also, since you said the alternative segment works, I've also sometimes run into problems when I copy / paste code. Try making sure there's nothing on the lines there and rewrite the code segment.

Upvotes: 0

PaulSkinner
PaulSkinner

Reputation: 628

As you've said that changing the code to the CodeIgniter inbuilt get query helper works, the only thing I see in that code that might cause an issue is that there is no space between the SELECT and the *.

This shouldn't cause an issue, but as there's nothing else to go on here, it's worth a shot.

Upvotes: 0

Related Questions