Лучшие практики для обработки ошибок от базы данных в CodeIgniter

Вы видите в первой строке ошибки: индекс: 2, размер: 2

if(list.size() >= 2) {
    temp = temp + list.get(i+1);
}

Таким образом, здесь условие выполняется, потому что размер списка равен 2.

temp = temp + list.get(i+1); -> вот проблема.

Вы говорите: get (i + 1), который равен 2, но индекс фактически равен 1. Первая итерация = i+1 => 0+1 = 1 Вторая итерация = i+1 => 1+1 = 2 ---> это то, где вы получаете вне границы.

5
задан imlouisrussell 31 December 2011 в 11:05
поделиться

1 ответ

We use in our project constructions like these:

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

but this undocumented functions and in next releases this could change. Of course you can simply use standard php for mysql error handle function:

mysql_errno()  
mysql_error()

internally CI use these functions.

As for me the best practice is use in custom Model's base class (BaseModel)

$this->db->_error_number();

and determine error, next throw exception with information error message taken from

$this->db->_error_message();

All your Model derived from BaseModel, and call method to check last request for db error and your Model must handle exception, and process it (may be additionally log), of course you can implement check result as result value and avoid of throwing exception.

10
ответ дан 13 December 2019 в 19:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: