Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter : $this->db->_error_message() message returns NULL

I have a query which gives me perfect results. But I need to send the mysql errors (if any) to UI. So I deliberately change column name to blah_text.

$qID = 13
$this->db->select('id AS optionID, blah_text AS option, is_correct AS isCorrect');
$oQuery = $this->db->get_where('xq_options', array('question_id' => $qID));
if($oQuery){
    $qnaArray['options'] = $oQuery->result_array();
}
else{
    //$qnaArray['error'] = $this->db->_error_message();
    echo "Error: " .  $this->db->_error_message();
}

All the get is Error:

I tried using native mysqli function mysqli_error($oQuery); but same O/P

Note: I have set 'db_debug' => FALSE in database config files. If I set it to TRUE, i do get the CI error message

Error Number: 1054 Unknown column 'blah_text' in 'field list' SELECT id AS optionID, blah_text AS option, is_correct AS isCorrect

How do I capture sql errors in a variable and send it to UI like $qnaArray['error']

EDIT: 'db_debug' is currently set to FALSE. I want the error message to be set in a variable. Not echoed on the screen.

like image 832
enemetch Avatar asked Sep 20 '26 20:09

enemetch


2 Answers

$this->db->_error_message(); will not execute when selecting data.

Use this

$qID = '13';
$query = $this->db->query(
            "SELECT id AS optionID, blah_text AS option, is_correct AS isCorrect 
             FROM table_name WHERE xq_options = $qID");
$result = $query->result_array();
$count = count($result);

if (!empty($count)) {
    return $result;
}
else{
    echo "No Data Found";
}
like image 62
Abdulla Nilam Avatar answered Sep 22 '26 08:09

Abdulla Nilam


Use

$e = $this->db->error(); // Gets the last error that has occured
$num = $e['code'];
$mess = $e['message'];

Info available in https://www.codeigniter.com/userguide3/database/queries.html search "Handling Errors"

like image 36
Omar Matijas Avatar answered Sep 22 '26 08:09

Omar Matijas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!