2015-10-21 124 views
0

如何提取或获取此数组内的数据?因为我有一个数据库错误的数据仅仅是一个数组..从数组对象中提取数据

这里的代码..

的welcome.php - 控制器

public function test() 
{ 
    $this->load->model('Crud'); 
    $username = $this->session->userdata('username'); 
    $data['sc_data'] = $this->select2($username); 
    $data2['t_data'] = $this->select($data); 
    $this->load->view('quiz', $data2); 
} 

private function select($data) 
{ 
    $result = $this->Crud->t_select($data); 
    return $result; 
} 

private function select2($data) 
{ 
    $result = $this->Crud->s_course($data); 
    return $result; 
} 

crud.php - 模型

public function t_select($data=array()) 
{ 
    print_r($data); 
    $this->db->select('fac_id'); 
    $this->db->where('id', $data); 
    $query = $this->db->get('tbl_subjects'); 
    return $query->result(); 

} 

public function s_course($data = array()) 
{ 
    $this->db->select('subject_id'); 
    $this->db->where('stud_id', $data); 
    $query = $this->db->get('tbl_student_subject'); 
    return $query->result(); 
} 

这是错误..

Array([sc_data] => Array([0] => stdClass Object( [subject_id] => 2)[1] => stdClass的对象([subject_id] => 3)))


数据库出错

错误编号:1054

未知列'阵”在 'where子句'

SELECT fac_id FROM tbl_subjects WHERE id = Array

文件名:C :/wamp/www/sample/application/models/crud.php

行号:42

回答

0

看起来你正在寻找一个“stud_id”或“身份证”与是一个数组的值。

您应修改您的语句:

this->db->where('stud_id', $data); 
$this->db->where('id', $data); 

选择特定的出“$数据”

0

的东西使用PHP代码,你可以把你的对象STD对象转换为数组成JSON对象,这将返回一个你的对象的字符串为json格式。

采取这一结果,并用真实的,在那里将转换为关联数组

$阵列= json_decode一个额外的参数进行解码(json_encode($ stdObject),TRUE);

+0

我应该在哪里放这段代码先生?进入控制器?或进入模型? –

+0

您必须将此代码放在$ data attr。在此之后编辑这一行: –

+0

之后,你的print_t函数将在php中打印你的数组。在数组中,你可以像这样获得你的数据:foreach $ data $ dat {echo $ dat [''subject_id“];} –