简体   繁体   中英

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.

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

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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