与codeigniter连接时出错:变量查询

时间:2014-05-21 20:30:38

标签: php mysql sql codeigniter join

我有两个表患者(姓名,id_paz)和访问(id_visita,id_paz,日期),1名患者可能有更多访问。如下所示,我检查错误:"未定义变量:查询"和#34;为foreach()"

提供的参数无效

型号:

function result_getall(){

    $this->db->select('*');
    $this->db->from('paziente');
    $this->db->join('visita', 'paziente.id_paz = visita.id_visita', 'left'); 
    $query = $this->db->get();
    return $query->result($sql);

    }

查看:

<?php foreach ($query as $row): ?>      

                   <?php echo $row->id_paz?><br>
               <?php echo $row->nome?><br>
               <?php echo $row->cognome?><br>


                    <?php echo $row->id_visita ?>
         <?php endforeach; ?>

控制器:

function getall(){      
    $this->load->model('visita_model');
    $data['query'] =$this->visita_model->result_getall();
    print_r($data['query']);
    die();
    $this->load->view('aggiungi_view', $data);
    }

1 个答案:

答案 0 :(得分:1)

也许你的阵列没有设定。
尝试使用print_r检查它或添加如下所示的if语句:

<?php 
    if(isset($query)){
        foreach ($query as $row): ?>      

                   <?php echo $row->id_paz?><br>
               <?php echo $row->nome?><br>
               <?php echo $row->cognome?><br>


                    <?php echo $row->id_visita ?>
         <?php endforeach; 
     }
     ?>

并在你的控制器中更改:

return $query->result($sql);

到此:

$return = array();
foreach ($query->result() as $row)
    array_push($return, $row);

return $return;