【问题标题】:When I update my data with PHP CodeIgniter, It gives an error. But data is saving当我用 PHP CodeIgniter 更新我的数据时,它给出了一个错误。但数据正在保存
【发布时间】:2018-02-17 17:08:32
【问题描述】:

我正在使用“CodeIgniter-3.1.3”。当我更新我的数据时,它会给出如下错误。但是我的数据保存在数据库中。我找不到问题所在。我的模型函数与版本相关有什么问题吗?

Error : Fatal error: Call to undefined method CI_DB_mysqli_driver::_error_number()

在控制器中

public function saveUpdateData(){
    $updateDataArray = array(
        "fname" => $_POST['fname'],
        "lname" => $_POST['lname'],
        "username" => $_POST['user'],
        "userlevel" => $_POST['userlevel'],
        "contact" => $_POST['contact_no']
    );
    $whereArr=array("user_id"=>$this->input->post("user_id"));
    $rst = $this->MyModel->updateData("admin", $updateDataArray, $whereArr);

    if ($rst) {
      echo "updated";
    }    
    else{
      echo "error";
    }
}

在模型中

function updateData($tablename, $data_arr, $where_arr) {
    try {
        $this->db->update($tablename, $data_arr, $where_arr);
        $report = array();
        $report['error'] = $this->db->_error_number();
        $report['message'] = $this->db->_error_message();
        return $report;
    } catch (Exception $err) {
        return $err->getMessage();
    }
}

enter image description here

【问题讨论】:

    标签: php codeigniter error-handling undefined fatal-error


    【解决方案1】:

    您尝试访问的功能已被弃用。

    替换:

    • $this->db->_error_number()
    • $this->db->_error_message()

    与:

    • $this->db->error()

    新函数将返回一个包含错误号和消息的数组。

    请参阅此 CodeIgniter 页面: https://www.codeigniter.com/userguide3/changelog.html?highlight=_error_number

    【讨论】:

      【解决方案2】:

      这样做

      在模型中

      function updateData($tablename, $data_arr, $where_arr) 
      {
          if (!$this->db->update($tablename, $data_arr, $where_arr)) {
              $result = $this->db->error(); 
          } 
          else {
              return TRUE;
          }
      }
      

      在控制器中

      $rst = $this->MyModel->updateData("admin", $updateDataArray, $whereArr);
      
      if ($rst == TRUE) {
        echo "updated";
      }    
      else{
        print_r($rst);
      }
      

      阅读Error Handling Codeigniter

      【讨论】:

      • 乐于助人:)
      【解决方案3】:

      在 CodeIgniter 3+ 中,您可以使用以下命令获取错误编号和消息: $error = $this->db->error();

      参考:Handling Query Errors in CodeIgniter

      【讨论】:

        猜你喜欢
        • 2021-04-02
        • 2017-07-28
        • 1970-01-01
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 2021-12-26
        相关资源
        最近更新 更多