php - Mysql 查询返回空数组?

标签 php codeigniter

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

我的 Controller

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

和这个 View

                <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 的空数组。 当我从我的模型中注释以下行时,它返回数据库中的所有值,那么这里实际发生了什么?是外键约束失效了还是别的原因??

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

最佳答案

型号

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

关于php - Mysql 查询返回空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322359/

相关文章:

javascript - 使用基于 Codeigniter 的 Jquery Java 刷新 html 表

php - 使用 mysql_query() 使用数字序列更新表行

php - 在 php 编码中将 php 连接到下拉列表

PHP + MySQL + HTML 在表格中显示不同的信息

php - Codeigniter join() 不起作用

CodeIgniter 中的 MySQL 错误

php - 我需要一些有关在 codeigniter 中使用 session 的帮助

php - 使用自定义条件在 PHP 中分解数据

php - PHP是否可以同时解析多个函数?

php - 在 PHP 中检查上传表单中的文件扩展名