codeigniter Mysql查询是否返回空数组?

luaexgnf  于 7个月前  发布在  Mysql
关注(0)|答案(2)|浏览(66)

我想有三个国家,州和城市的下拉唐斯,我想从数据库中填充它们。我成功地检索了国家下拉列表的值,但问题是州下拉列表中的值根据选定的国家显示。作为一个例子,如果我选择U.S.A,我只想得到美国的州,我有两个表countriesstates,我有country_id作为state表中的外键,实际上这就是我的问题所在。
我的代码-控制器:

// call to fill the second dropdown with the states 
public function buildDropStates()  
{  
      // set selected country id from POST  
      echo $id_country = $this->input->post('country_id');  
      // var_dump($id_country);
      // run the query for the cities we specified earlier  
      $districtData['districtDrop']=$this->Country_states_cities->getCityByCountry($id_country);  
      // var_dump($districtData);
      $output = null;  

      foreach ($districtData['districtDrop'] as $row)  
      {  
         // here we build a dropdown item line for each  query result  
         $output .= "<option value='".$row->state_id."'>".$row->state_name."</option>";  
      }  

      echo $output;  
      // var_dump($output);
}

字符串
我的模型

//fill your state dropdown depending on the selected country  
       public function getCityByCountry($country_id=string)  
       {  
            $this->db->select('state_id,state_name');  
            $this->db->from('nm_state');  
            $this->db->where('country_id',$country_id);  
            $query = $this->db->get();  
            //var_dump($query);
            var_dump($query->result());
            return $query->result();   
       }  
    }


这是一个观点:

<script>
                    $(document).ready(function() {  
                 $("#country").change(function(){  
                 /*dropdown post *///  
                 $.ajax({  
                    url:"<?php echo  
                    base_url();?>Country_state/buildDropStates",  
                    data: {id:  
                       $(this).val()},  
                    type: "POST",  
                    success:function(data){  
                    $("#state").html(data);  
                 }  
              });  
           });  
        });   
        </script>
                <label for="country">Country</label>
                     <?php echo form_dropdown('country',$countryDrop,'','class="required" id="country" '); ?>
                     <br />
                     <br />
                    
                    
                    <label for="state">State</label>
                     <select name="state" id="state">  
                         <option value="">Select</option>  
                      </select>


问题是我的模型getCityByCountry()返回一个大小为0的空数组。
当我从模型中注解下面一行时,它返回了DB中的所有值,那么这里到底发生了什么?是外键约束不起作用还是其他原因?

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


回显查询时

CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 2 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.24 [server_version] => 50624 [stat] => Uptime: 2694 Threads: 1 Questions: 6365 Slow queries: 0 Opens: 88 Flush tables: 1 Open tables: 81 Queries per second avg: 2.362 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 715 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 2 [lengths] => [num_rows] => 0 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )

ghhaqwfi

ghhaqwfi1#

型号

function get_all_where($table, $condition,$order_by=array())
{
    if(!empty($order_by))
    {
        $this->db->order_by($order_by['field'], $order_by['type']);
    }
    $query = $this->db->get_where($table, $condition);

    if ($query->num_rows() > 0)
    {
        return $query->result_array();
    }
}

字符串

控制器

$result= $this->classModel->get_all_where('nm_state', array('country_id' => $country_id));

yzckvree

yzckvree2#

您正在回显$id_country,并且函数getCityByCountry没有获取参数,因此没有返回结果,请删除回显

//set selected country id from POST  
  $id_country = $this->input->post('country_id');  
  //var_dump($id_country);
  //run the query for the cities we specified earlier  
          $districtData['districtDrop']=$this->Country_states_cities->getCityByCountry($id_country);

字符串

相关问题