3

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.

9
  • If you are getting error using 'db_debug'=> TRUE. What is the problem? Commented Oct 30, 2015 at 13:36
  • @SagarKhatri I get errors on the page CI styled. I need to send it as a webservice json to UI so that I can be displayed properly. Commented Oct 31, 2015 at 8:46
  • Which clients are going to use your WebService? Android,iPhone or in frontend through JS? Commented Oct 31, 2015 at 10:43
  • @SagarKhatri iPad and JS Commented Oct 31, 2015 at 10:49
  • I think stripping the tag and getting the message text will be enough. You can use $qnaArray['error'] = strip_tags($this->db->_error_message());. Commented Oct 31, 2015 at 10:53

4 Answers 4

1

$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";
}
Sign up to request clarification or add additional context in comments.

5 Comments

I can use $query->num_rows() instead. Thats not the point. I need to see the MySQL error message. Unknown column 'blah_text' in 'field list' SELECT id AS optionID, blah_text AS option, is_correct AS isCorrect
Use my query and check
Your query works. But I either get results or I get "No Data Found", What I need is the error message.
Unknown column 'blah_text' in 'field list' SELECT id AS optionID, blah_text AS option, is_correct AS isCorrect
@eNeMetcH ahh use this inside else $this->db->_error_message();
1

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"

1 Comment

Actually the documentation says; " the error() method will return an array containing its code and message". So, the way you get the code and message is $error['code'] $error['message']
0

Instead of this:

if($oQuery)
{
   $qnaArray['options'] = $oQuery->result_array();
}
else{
//$qnaArray['error'] = $this->db->_error_message();
echo "Error: " .  $this->db->_error_message();
}  

try using this:

if($oQuery->num_rows()>0)
{
   $qnaArray['options'] = $oQuery->result_array();
}
else{
//$qnaArray['error'] = $this->db->_error_message();
echo "Error: " .  $this->db->_error_message();
}    

2 Comments

I got this: Fatal error: Call to a member function num_rows() on boolean But a CI error message. Not from $this->db->_error_message();
0
if (!$this->db->simple_query('SELECT `id` FROM `customers`')) {
   $error = $this->db->error();
}

$data['db_error_message'] = 'Error [' . $error['code'] . '] ' . $error['message'];

From the documentation on page https://www.codeigniter.com/userguide3/database/queries.html#handling-errors

In your view, this code prints out a message like this;

DB Error Message : Error [1146] Table 'mydb.customers' doesn't exist

Similarly, your code prints out the same result.

$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) {
   $err = $this->db->error();
}
$data['err_message'] = 'Error [' . $err['code'] . '] ' . $err['message'];

prints; Err Message : Error [1146] Table 'mydb.xq_options' doesn't exist

I did not create tables, so my messages are 'table not found' error messages instead of 'column not found'.

I guess this is what you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.