简体   繁体   English

codeigniter SELECT查询功能

[英]codeigniter SELECT query function

I am new to codeigniter and trying to access data from mysql database 我是Codeigniter的新手,并尝试从mysql数据库访问数据

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. 一个是model_data,另一个是model_data_cat。 but i get error "Parse error: syntax error, unexpected $end in " 但我收到错误“解析错误:语法错误,意外出现$ 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 您可以在config文件夹的自动加载中调用库

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循环访问,例如

foreach($res as $key){ $value = $res->result(); foreach($ res作为$ 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 * . 正如您已经说过的那样,将代码更改为内置的CodeIgniter内置的get query helper可以正常工作,我在该代码中看到的唯一可能引起问题的地方是SELECT*之间没有空格。

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. 意外的$ end几乎总是意味着您错过了代码中某处的结尾花括号,括号,引号等,并且不在该位置。

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. 尝试确保那里的所有行都没有,然后重写代码段。

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

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