Codeigniter - Continue on SQL Error

CodeIgniter - continue on SQL error?

Yeah, took me a while too and annoyed the hell out of me:

$db['default']['db_debug'] = FALSE;

... in config/database.php - disables the error page.

After a query ran, use this to check for an error:

if (!empty($this->db->_error_message())) {
echo "FAIL";
}

CodeIgniter - how to catch DB errors?

Use error() method:

$this->db->error(); 

For CodeIgniter 2, you can use the following functions which are now deprecated:

$this->db->_error_message(); (mysql_error equivalent)
$this->db->_error_number(); (mysql_errno equivalent)

Continue execution even if hostname is wrong for db in codeigniter

You could create 3 groups of database like:

$db['default'] = array(
'hostname' => '1.1.1.1',
'username' => 'username',
'password' => 'password',
'database' => 'DB',
'dbdriver' => 'mysqli',
'dbprefix' => 'pre_',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['db2'] = array(
'hostname' => '1.1.1.2',
'username' => 'username2',
'password' => 'password',
'database' => 'DB',
'dbdriver' => 'mysqli',
'dbprefix' => 'pre_',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['db3'] = array(
'hostname' => '1.1.1.3',
'username' => 'username3',
'password' => 'password',
'database' => 'DB',
'dbdriver' => 'mysqli',
'dbprefix' => 'pre_',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

And connect each database:

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('db2', TRUE);
$DB3 = $this->load->database('db3', TRUE);

So you can use each for your needs:

$DB1->query();
$DB1->result();

https://codeigniter.com/user_guide/database/connecting.html#connecting-to-multiple-databases

Database Error Handling problem in CodeIgniter

You need to throw an exception if there was some mysql error:

try {
$query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'";
$result = $this->db->query($query_str);

if (!$result)
{
throw new Exception('error in query');
return false;
}

return $result;

} catch (Exception $e) {
return;
}

Codeigniter - Fatal error to connect MySQL and SQL Server

I've run into many problems in the past using the the straight up sqlsrv driver with CodeIgniter.

I would convert your sqlsrv to pdo_sql_srv by doing the following in your databbase.php configuration file:

$db['biodb'] = array(
'hostname' => '192.168.20.231',
'username' => 'abzalali',
'password' => '',
'database' => 'pdata',
'dbdriver' => 'pdo',
'subdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'autoinit' => TRUE,
'stricton' => FALSE,
);

I'm also surprised you aren't getting an undefined var error on your index method in your controller. Try changing it to this:

public function index() {
//you need to call the database property you
//created in your construct which appears to be sqlsrvr
$query = $this->sqlsrvr->get('arb_protest_pp');
foreach ($query->result() as $row) {
echo $row->id;
}
}

Codeigniter 3: Can't catch database error using try catch block

As for CI 3, below code gets database error code and error message. db_debug is set to FALSE.

public function add() {
try {
$this->db->trans_start(FALSE);
$this->db->insert('users', $preparedData);
$this->db->trans_complete();

// documentation at
// https://www.codeigniter.com/userguide3/database/queries.html#handling-errors
// says; "the error() method will return an array containing its code and message"
$db_error = $this->db->error();
if (!empty($db_error)) {
throw new Exception('Database error! Error Code [' . $db_error['code'] . '] Error: ' . $db_error['message']);
return false; // unreachable retrun statement !!!
}
return TRUE;
} catch (Exception $e) {
// this will not catch DB related errors. But it will include them, because this is more general.
log_message('error: ',$e->getMessage());
return;
}
}

Refer to documentation at https://www.codeigniter.com/userguide3/database/queries.html#handling-errors

saying

If you need to get the last error that has occurred, the error() method will return an array containing its code and message.

It is a bit incomplete in my opinion because it does not show error code and error message in the example code.



Related Topics



Leave a reply



Submit