简体   繁体   English

如何从CodeIgniter中的失败数据库查询中恢复?

[英]How to recover from failed database query in CodeIgniter?

In CodeIgniter, if your sql query fails, then the script stops running and you get an error. 在CodeIgniter中,如果您的sql查询失败,则脚本停止运行并且您收到错误。 Is there any way to do it so you can try a query, and if it fails, then you silently detect it and try a different query without the user knowing that the query failed? 有没有办法做到这一点,所以你可以尝试查询,如果它失败,那么你静静地检测它并尝试不同的查询,而用户不知道查询失败?

You could modify the Exceptions class to... throw an exception. 您可以将Exceptions类修改为...抛出异常。 Just create MY_Exceptions.php in application/core/ : 只需在application/core/创建MY_Exceptions.php

class MY_Exceptions extends CI_Exceptions {

    function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        // BEGIN EDIT
        if ($template === 'error_db')
        {
            throw new Exception(implode("\n", (array) $message));
        }
        // END EDIT

        set_status_header($status_code);

        $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush();
        }
        ob_start();
        include(APPPATH.'errors/'.$template.'.php');
        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }
}

Then use a try/catch block to check for the error and attempt to run another query: 然后使用try / catch块检查错误并尝试运行另一个查询:

try {
    $this->db->get('table1');
} catch (Exception $e) {
    $this->db->get('table2');
}

It's kind of a sloppy workaround, but it gets the job done. 这是一种草率的解决方法,但它完成了工作。

You might want to take a look at transactions as well: 您可能还想查看交易

Running Transactions 运行事务

To run your queries using transactions you will use the $this->db->trans_start() and $this->db->trans_complete() functions as follows: 要使用事务运行查询,您将使用$this->db->trans_start()$this->db->trans_complete()函数,如下所示:

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

You can run as many queries as you want between the start/complete functions and they will all be committed or rolled back based on success or failure of any given query. 您可以在启动/完成功能之间运行任意数量的查询,并且它们将根据任何给定查询的成功或失败提交或回滚。

One of the ways to achieve this is 实现这一目标的方法之一是

First. 第一。

Set  ['db_debug'] = FALSE; in config/database.php

Then, 然后,

In your model - 在你的模型中 -

public function attempt_one($data) {
  //build your query ....
  $query_result = $this->db->insert('table_name');

  if(!$query_result) {
     $this->error = $this->db->_error_message();
     $this->errorno = $this->db->_error_number();
     return false;
  }
  return $something;
}

public function attempt_two() {
  //another query goes in here ...
}

in your controller - 在你的控制器 -

public function someAction ()
{
  //some code 
  $data = $some_data;
  $result1 = $this->yourmodel->attempt_one($data);
  if($result1 === false)
  {
    //Some code to send an email alert that first query failed with error message 
    //and/or log the error message/ number 
    $result2 = $this->yourmodel->attempt_two($data);
  }

}

Just use 只是用

$this->db->simple_query() 

rather than 而不是

$this->db->query()

Simple Query will return true/false. Simple Query将返回true / false。

您可以使用以下方法访问数据库

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

您可以从他们的官方论坛中查看此主题,讨论并建议针对此特定主题的解决方案: http//codeigniter.com/forums/viewthread/76524/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM