dongyi1524 2011-07-14 15:26
浏览 31

如何将此代码中的错误转换并修复为CodeIgniter?

how is convert this code to CodeIgniter code:

search_hotel: -> this is CI_Model

return mysql_query("select * from hotel_submits where name LIKE '".$searchterm."'")

i try but have error:

$query = $this->db->order_by("id", "desc")->like('name', '$searchterm')->get('hotel_submits');
        return $query->row();

error:

A PHP Error was encountered
Severity: Warning
Message: mysql_fetch_assoc() expects parameter 1 to be resource, array given
Filename: admin/tour.php
Line Number: 15


A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: admin/tour.php
Line Number: 21

code:-> this is CI_Controller

$searchterm = $this->input->post('search_hotel');
$result = $this->model_tour->search_hotel($searchterm);
while ($row = mysql_fetch_assoc($result)) { //this is line 15
//giving names to the fields
$data = array (
   'name' => $row->name,            
);
}
echo json_encode($data);  //this is line 21
  • 写回答

1条回答 默认 最新

  • douchen1924 2011-07-14 15:35
    关注

    Think there's various things here. In your model where you return $query->row() you are only going to return one recordset row. You should try $query->result() instead. I would also advise moving the code you have in your controller into the model as well. So you model would begin to look something like:

    function search_hotel($searchterm)
    {
    
    $query = $this->db->order_by("id", "desc")->like('name', $searchterm)->get('hotel_submits');
    
    $data = array();
    
    foreach ($query->result() as $row)
    {
       data[] = $row->name
    }
    
    return $data
    }
    

    So your controller simple becomes something like:

    function your_controller() {
    //Set your $search_term somehow
    
        echo json_encode($this->model('your_model_name')->search_hotel($search_term);
    }
    

    Hopefully that should start to point you on the right track. Full information on creating and using recordsets can be found within the CI docs - http://codeigniter.com/user_guide/database/index.html

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行