【问题标题】:Mysql query is returning empty array?Mysql查询返回空数组?
【发布时间】:2015-11-26 03:27:19
【问题描述】:

我想要三个国家、州和城市的下拉列表,我想从数据库中填充它们。我成功地检索了国家下拉列表的值,但问题是状态下拉列表,其中值根据所选国家显示。例如,如果我选择 U.S.A,我只想获得美国的州。 我有 2 个表国家和州,我在状态表中有 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);

关于回显查询

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

【问题讨论】:

  • 如果我清楚地理解了您的问题...如果您在下拉列表中选择一个国家/地区,则另一个下拉列表将显示该国家/地区的所有州?对吗?
  • @ShiguriAnemone' 是的,但问题是查询返回状态,因为在我的模型中返回空数组[结果 var_dump($query->result());]
  • 您可以使用Ajax.Post.. 创建一个onchange.. 的事件,然后根据所选值执行select 查询......
  • @ShiguriAnemone' ya 这也是替代方案,但现在的问题是为什么我的查询返回空数组
  • 其他地方应该有execute() 代码吗?

标签: php codeigniter


【解决方案1】:

型号

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

【讨论】:

  • 这个答案缺少教育解释。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 2023-04-03
    • 1970-01-01
    • 2016-03-24
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    相关资源
    最近更新 更多