[英]select all and some attribute from a table get different result in codeigniter
I've try to get result from sql query in codeigniter with mode select all attribute but it returns a PHP error but when I specify some attribute in give the right answer but the query is too long to be written. 我已经尝试通过模式选择所有属性从codeigniter中的sql查询中获取结果,但是它返回PHP错误,但是当我在给出正确答案时指定了一些属性,但是查询时间太长而无法编写时。
This 2 mode select that I've try and give different result. 我尝试过的2种模式选择会给出不同的结果。
First 第一
class model_kemiskinan extends CI_Model { class model_kemiskinan扩展CI_Model {
..... //constructor here ..... //构造函数在这里
function get_kemiskinan_provinsi(){
$this->tahun = "2011";
$this->kodeProv = "31";
$this->query = "select * from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
$this->result = $this->db->query($this->query);
return $this->result->result();
}
Then the controlller pass it 然后控制器通过
public function testquery(){
$this->load->model('model_kemiskinan');
$data['hasil'] = $this->model_kemiskinan->get_kemiskinan_provinsi();
$data['main_content'] = 'test';
$this->load->view('template', $data);
}
and the view 'test' respond it with these code: 并在视图“测试”中通过以下代码对其进行响应:
if(is_array($hasil)){
foreach ($hasil as $baris ) {
echo $baris->tahun;
echo $baris->id_provinsi;
echo "<br/>";
}
And the result is this 结果是这样
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$tahun
Second 第二
But if I change the select mode that look like this : 但是,如果我更改如下所示的选择模式:
$this->query = "select tahun, id_provinsi from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
It will work properly 它将正常工作
Is there any solution about select all mode? 关于全选模式有什么解决方案吗?
-Thanks before- -感谢之前-
like the documentation ( http://ellislab.com/codeigniter/user-guide/database/examples.html ) says: 就像文档( http://ellislab.com/codeigniter/user-guide/database/examples.html )所示:
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $query->num_rows();
you get only one row by calling result() so you need to call it by foreach: 通过调用result()只会得到一行,因此您需要通过foreach来调用它:
function get_kemiskinan_provinsi(){
$this->tahun = "2011";
$this->kodeProv = "31";
$this->query = "select * from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
$this->result = $this->db->query($this->query);
$res = array();
foreach ($this->result->result() as $row)
{
$res[] = $row;
}
return $res;
}
Please recognize that the fields in the result object are case sensitive! 请注意结果对象中的字段区分大小写! If you have a field named "Tahun" in your db then "select tahun ..." will work in mysql and will give you an object where you can access $res->tahun.
如果您的数据库中有一个名为“ Tahun”的字段,则“ select tahun ...”将在mysql中工作,并将为您提供一个对象,您可以在其中访问$ res-> tahun。
If you do "select * ...." then you can only access it like this: $res->Tahun (with capial T) 如果您执行“ select * ....”,则只能按以下方式访问它:$ res-> Tahun(带大写字母T)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.