Mysql查询返回空数组?

时间:2015-09-01 01:47:14

标签: php mysql codeigniter

我希望有三个国家,州和城市的下拉菜单,我想从数据库中填充它们。我成功地恢复了各国贬值的价值,但问题是国家下降,根据所选国家显示价值。例如,如果我选择美国,我只想在美国获得州 我有两个表国家和州,我有country_id作为状态表中的外键实际上我的问题是。我的代码:

我的控制器

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

on echoing query

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] => ) 

2 个答案:

答案 0 :(得分:1)

<强> MODEL

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

<强> CONTROLLER

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

答案 1 :(得分:0)

你正在回显$ 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);