WHERE子句之间的MYSQL倍数

时间:2015-12-05 07:39:11

标签: mysql codeigniter

这给了我错误,哪里可能是问题?

错误号码:1064

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第3行的''附近使用正确的语法

select HouseBlock.*, HouseDetails.* from HouseBlock inner join HouseDetails on HouseBlock.ID=HouseDetails.HouseID where HouseBlock.LocCode = ATH and HouseDetails.HouseID =

行号:3是

where HouseBlock.LocCode = ' .$LocCode .' and HouseDetails.HouseID = ' .$HouseID);

模型     

function __construct() {
    parent::__construct();
}


function getdb($LocCode,$HouseID) {
$query = $this->db->query('select HouseBlock.*, HouseDetails.* 
        from HouseBlock inner join HouseDetails on HouseBlock.ID=HouseDetails.HouseID
        where HouseBlock.LocCode = "' .$LocCode .'" and HouseDetails.HouseID = ' .$HouseID);
    if($query->result_array()){
        return $query->result_array();
    }
    else
    {
    $query = $this->db->query('select *
                    from HouseDetails
                    where HouseID = ' .$HouseID);
        return $query->result_array();
    }

}

function getlocation(){
$query = $this->db->query('select * from LocationMaster');
            return $query->result_array();
} 

function getblock(){ 
    $query = $this->db->query('select * from HouseBlock');
            return $query->result_array();
    } 

function updatedb($EmpNo,$HouseID,$HouseNo) {
    foreach($HouseID as $key=>$row) {
    $array = array('ID'=>$row,
        'EmpNo'=>$EmpNo[$key],
        'HouseID'=>$HouseID[$key],
        'HouseNo'=>$HouseNo[$key]
        );
    $this->db->where('ID', $key);
    $this->db->update('HouseDetails', $array);
    }
}



function adddb($LocCode,$BlockName,$HouseID,$HouseNo,$EmpNo) {
        for($i=1;$i<=2;$i++) {
        if($BlockID[$i] != 0) {
        $data = array(
        'LocCode' => $LocCode,
        'BlockName' => $BlockName,
        'HouseID' => $HouseID[$i],
        'HouseNo' => $HouseNo[$i],
        'EmpNo' => $EmpNo[$i]
        );
        $this->db->insert('HouseDetails', $data);
        }
    }
}

控制器

class Housing扩展了CI_Controller {

    public function Index() {
    if($this->input->post('cancel')){
    $this->newform();
    }
    elseif($this->input->post('delete')){
    $this->load->model('Housing_model');
    $this->Housing_model->deletedb($this->input->post('delete'));
    $this->getdb($this->input->post('LocCode'));
            }
    elseif($this->input->post('update')){
            $this->load->model('Housing_model');
        if($this->input->post('HouseNo')){
        $this->Housing_model->updatedb(
                       $this->input->post('HouseNo'),
                           $this->input->post('EmpNo'));
        }
        $this->Housing_model->adddb(
                $this->input->post('LocCode'),
                $this->input->post('BlockName'),
                $this->input->post('AddHouseNo'),
                $this->input->post('AddEmpNo'));
        $this->getdb($this->input->post('LocCode'));
        }
        elseif($this->input->post('LocCode')){
    $this->getdb($this->input->post('LocCode'),$this->input->post('BlockName'));
    }
    else{
    $this->newform();
    }
    }

function newform()
    {
    $this->load->model('Housing_model');
            $this->load->view('header');
            $this->load->view('topnav');
        $data['getlocation'] = $this->Housing_model->getlocation();
        $data['getblock'] = $this->Housing_model->getblock();
            $this->load->view('Housing_form',$data);
            $this->load->view('footer');

    }

     function getdb($LocCode, $HouseID)
    {
            $this->load->model('Housing_model');
            $this->load->view('header');
            $this->load->view('topnav');
            $data['housingformdata'] = $this->Housing_model->getdb($LocCode,$HouseID);
            $this->load->view('Housing_form',$data);
            $this->load->view('footer');
    }

}

2 个答案:

答案 0 :(得分:0)

那么您的locCode字段是varchar?

试试这个(缺少""

$query = $this->db->query("select HouseBlock.*, HouseDetails.* 
            from HouseBlock inner join HouseDetails on HouseBlock.ID=HouseDetails.HouseID
            where HouseBlock.LocCode = '" .$LocCode ."' and HouseDetails.HouseID = ".$HouseID."");

答案 1 :(得分:0)

试试这个

$query = $this->db->query(
    'SELECT HouseBlock.*, HouseDetails.* 
    FROM HouseBlock 
    INNER JOIN HouseDetails ON ouseBlock.ID=  HouseDetails.HouseID
    WHERE HouseBlock.LocCode = $LocCode AND HouseDetails.HouseID = $HouseID');
if($query->result_array()){
    return $query->result_array();
}
else
{
    $query = $this->db->query(
                'SELECT *
                FROM HouseDetails
                WHERE HouseID = $HouseID');
        return $query->result_array();
}