从Codeigniter返回Unknown列

时间:2014-11-13 10:03:55

标签: codeigniter

我试图根据XML文件中的WHERE条件获取一些数据,但问题是请求foreach条件它返回的值VALUE不存在。

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<COUNTRY>
  <STATE city="NewYork" available="1"/>
  <STATE city="NewJersey" available="1"/>
</COUNTRY>

控制器:

public function getIdFromCity($city){

        $this->db->select('entity_id');
        $this->db->where('city', $city);
        $query = $this->db->get('cities');
        return $query->result_array();

    }


        $count   = 1;
        foreach ($xml->STATE as $state){
            $data       = array();
            $data[0]    = $state->attributes()->city;
            $data[1]    = $state->attributes()->available;

            if($this->getIdFromCity($data[0])){
                echo '<pre>';
                print_r($this->getIdFromCity($data[0]));
            }
        $count++;
        }

问题是,我怎样才能从基于XML数据条件的城市数据库中获取数据?

错误是:

Error Number: 1054

Unknown column 'NewYork' in 'where clause'

SELECT `entity_id` FROM (`cities`) WHERE `city` = NewYork

Filename: codeigniter\system\database\DB_driver.php

Line Number: 330

2 个答案:

答案 0 :(得分:0)

您的查询生成城市= Newyork
但它应该是city =&#34; Newyork&#34;
将行$this->db->where('city', $city);替换为

$this->db->where("city = '".$city."'");  

$this->db->where("city = '$city'");

确保在&#34; =&#34;之前和之后都有空间。登录

答案 1 :(得分:0)

替换此行

$this->db->where('city', $city);

$this->db->where('city', $city, TRUE);