简体   繁体   中英

codeigniter inner join error

I want to equal some table with Inner Join in Codeigniter. But I give some errors. I put below some codes in my controller. But I give an error like that:

A Database Error Occurred Error Number: HY000/1096 No tables used SELECT * Filename: C:/xampp/htdocs/erp/system/database/DB_driver.php Line Number: 691

My Controller Inner Join Here:

    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);

}

In view :

<?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 }} ?>

Hope this will help you:

Use ci query builder to get the desired result

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);
}

Your view should be like this :

<?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 }  ?>

For more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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