Codeigniter内部联接错误

时间:2018-06-29 18:39:00

标签: php sql codeigniter inner-join codeigniter-3

我想让Codeigniter中的Inner Join等于某个表。但是我给出了一些错误。我在控制器中放了一些代码。但是我给出了这样的错误:

  

发生数据库错误     错误编号:HY000 / 1096     没有使用表格     选择 *     文件名:C:/xampp/htdocs/erp/system/database/DB_driver.php       行号:691

我的控制器内部加入此处:

    public function edit($cusId) {

    $this->lang->load('content', $this->session->userdata('people_lang'));

    $viewData = new stdClass();
    $viewData->customer = $this->db->where("cusId", $cusId)->get("customer")->row();
    $viewData->doctype = $this->db->get("documenttype")->result();
    $viewData->documents = $this->db->get("document")->result();

    $this->db->select('dt.docTypeId,dt.docTypeCat,dt.docTypeNameEn,
 dt.docTypeNameAr,d.docFileDocType,');
    $this->db->from('documenttype dt');
    $this->db->join('document d' ,'d.docFileDocType = dt.docTypeId');

    $query = $this->db->get();
    if ($query->num_rows() > 0 )
    {
        $viewData['document'] = $query->row();
    }

    $this->load->view("file_manager", $viewData);

}

视图中:

<?php
foreach($documents as $docs){ 
    if(($customer->cusId) == ($docs->docFileCusId)){ ?>
<tr>
    <td data-title="File ID"><?php echo $docs->docFileId; ?></</td>
    <td data-title="Customer Code"><?php echo $customer->cosCode; ?></td>
    <td data-title="Name Surname"><?php echo $customer->cosName.' '.$customer->cosSurname; ?></</td>
    <td data-title="File Type"><?php echo $document->docFileDocType; ?></td>
    <td data-title="Description"><?php echo $docs->docFileDesc; ?></td>
    <td data-title="Create Date"><?php echo $docs->docFileCreateDate; ?></</td>
    <td data-title="File"><a href="<?php echo base_url().'upload/file/customer/'.$docs->docFileDirectory; ?>" download>
    <?php echo $docs->docFileDirectory; ?></a> <i class="fa fa-download"></i></td>
</tr>
<?php }} ?>

1 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

使用ci查询生成器获得所需结果

public function edit($cusId) 
{
   $this->lang->load('content', $this->session->userdata('people_lang'));

   $viewData = array();

   $this->db->select('*');
   /*$this->db->select('dt.docTypeId,dt.docTypeCat,dt.docTypeNameEn,
     dt.docTypeNameAr,d.docFileDocType');*/
   $this->db->from('documenttype dt');
   $this->db->join('document d' ,'d.docFileDocType = dt.docTypeId');
   $this->db->join('customer c' ,'c.cusId = d.docFileId');
   $this->db->where("c.cusId", $cusId)

   $query = $this->db->get();
   if ($query->num_rows() > 0 )
   {
      $viewData['documents'] = $query->result();
   }

   /*print viewData here to make sure it has data or not 
     print_r($viewData);die;
   */
   print_r($viewData);die;
   $this->load->view("file_manager", $viewData);
}

您的视图应如下所示:

<?php
   foreach($documents as $docs){ ?>
   <tr>
     <td data-title="File ID"><?php  echo $docs->docFileId; ?></</td>
     <td data-title="Customer Code"><?php  echo $docs->cosCode; ?></td>
     <td data-title="Name Surname"><?php  echo $docs->cosName.' '.$docs->cosSurname;  ?></</td>
     <td data-title="File Type"><?php  echo $docs->docFileDocType; ?></td>
     <td data-title="Description"><?php  echo $docs->docFileDesc; ?></td>
     <td data-title="Create Date"><?php  echo $docs->docFileCreateDate; ?></td>
     <td data-title="File">
        <a href="<?php echo base_url().'upload/file/customer/'.$docs->docFileDirectory;?>" download><?php  echo $docs->docFileDirectory; ?>
        </a> <i class="fa fa-download"></i>
    </td>
  </tr>
<?php }  ?>

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data