簡體   English   中英

問題在 mysql 查詢在 codeigniter 只有當我添加 if 條件

[英]issue in mysql query in codeigniter only if I add if condition

在下面的代碼中,每當我在 if 條件下添加下面的代碼時,我都會收到錯誤消息

if($this->ion_auth->is_customer())
      $this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));
    $this->db->select('company.*, cities.name as company_city, states.name as company_state, countries.name as company_country');
    $this->db->from('company as company');
    $this->db->join(CITIES.' as cities','cities.id = company.company_city_id' ,'left');
    $this->db->join(STATES.' as states','states.id = company.company_state_id' ,'left');
    $this->db->join(COUNTRIES.' as countries','countries.id = company.company_country_id' ,'left');
    $this->db->join(COMPANY_DATABASE.' as company_database','company_database.cdb_company_id = company.company_id' ,'left');

    if($this->ion_auth->is_customer())
      $this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));

    $this->db->where('company.company_delete_status',NOT_DELETED);    

    $query = $this->db->get();

    echo '<pre>';
    echo $this->db->get_compiled_query();
    print_r($query->result());
    echo $this->db->last_query();

上面查詢的問題是什么?

我遇到以下與查詢相關的問題

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `company_database`.`cdb_customer_id` = '19' AND `company`.`company_delete_' at line 2

SELECT * WHERE `company_database`.`cdb_customer_id` = '19' AND `company`.`company_delete_status` = 0

Filename: controllers/Test.php

Line Number: 112

您的 where 子句中沒有“from”子句。

select * {from company} where 'company_database'.'cdb_customer_id' =....

我懷疑 function

$this->ion_auth->is_customer()

可能正在調用另一個數據庫查詢,這幾乎完成了您在上面開始的查詢,一旦完成,它就會在 $this->db 之后僅使用 where 子句。

要修復調用 $this->ion_auth->is_customer() 之前你做 $this->db->select 然后在 IF 語句中只需使用返回的 boolean 這樣你就不會在你的時候再次調用查詢形成另一個查詢。

例子:

--ADD THIS LINE
$bIsClient = $this->ion_auth->is_customer();

$this->db->select('company.*, cities.name as company_city, states.name as company_state, countries.name as company_country');
    $this->db->from('company as company');
    $this->db->join(CITIES.' as cities','cities.id = company.company_city_id' ,'left');
    $this->db->join(STATES.' as states','states.id = company.company_state_id' ,'left');
    $this->db->join(COUNTRIES.' as countries','countries.id = company.company_country_id' ,'left');
    $this->db->join(COMPANY_DATABASE.' as company_database','company_database.cdb_company_id = company.company_id' ,'left');

    --AND CHANGE THIS 
    if($bIsClient)
      $this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));

    $this->db->where('company.company_delete_status',NOT_DELETED);    

    $query = $this->db->get();

    echo '<pre>';
    echo $this->db->get_compiled_query();
    print_r($query->result());
    echo $this->db->last_query();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM