CodeIgniter Forums
Error Handling with very basic SQL query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Error Handling with very basic SQL query (/showthread.php?tid=73407)



Error Handling with very basic SQL query - aslamdoctor - 04-21-2019

Hi Guys,

I have built this simple code which uses a specific database configuration and then runs SELECT query on the EMPLOYEE table.
This table has very huge database and I believe because of that, the db is throwing error. Which is fine.  
Below is my code. I have done the error handling code in this but it still throws attached error on browser. I want it to suppress throwing error on browser and catch it into variable so I can display it properly. Because I would like to use this code for an API server.

Ignore the part where the code is commented for $DB_MYSQL. That is for future use, I am going to switch database on specific occassions.

PHP Code:
$DB_MSSQL $this->load->database('mssql_db'TRUE);
$DB_MSSQL->db_debug FALSE;

//$DB_MYSQL = $this->load->database('mysql_db', TRUE);
//$DB_MYSQL->db_debug = FALSE;

$sql "SELECT * from EMPLOYEES";
$result  $DB_MSSQL->query($sql);

echo 
$sql.'<hr/>';

if(
$result){
 
$row $result->result_array();
 
 
$count=1;
 foreach(
$row as $record){
 echo 
"[ $count ] ";
 
var_dump($record);
 echo 
'<hr/>';
 
$count++;
 
 
ob_flush(); flush();
 }
}
else{
 
$error $DB_MSSQL->_error_message();
 
var_dump($error);




RE: Error Handling with very basic SQL query - InsiteFX - 04-21-2019

CodeIgniter User Guide - Database

PHP Code:
if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`'))
{
 
       $error $this->db->error(); // Has keys 'code' and 'message'




RE: Error Handling with very basic SQL query - aslamdoctor - 04-21-2019

(04-21-2019, 09:43 AM)InsiteFX Wrote: CodeIgniter User Guide - Database

PHP Code:
if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`'))
{
 
       $error $this->db->error(); // Has keys 'code' and 'message'


Nope, its still the same error. I already tried this before.


RE: Error Handling with very basic SQL query - php_rocs - 04-21-2019

@aslamdoctor,

Specifically, what database error is being given? Also, have you run your query directly in the database to make sure that it works? Also, you may be maxing out the expected output so you may need to reset a PHP ini setting (post_max_size or memory_limit). Just a thought. Have you checked the CI logs?


RE: Error Handling with very basic SQL query - aslamdoctor - 04-21-2019

(04-21-2019, 11:08 AM)php_rocs Wrote: @aslamdoctor,

Specifically, what database error is being given?  Also, have you run your query directly in the database to make sure that it works?  Also, you may be maxing out the expected output so you may need to reset a PHP ini setting (post_max_size or memory_limit).  Just a thought.  Have you checked the CI logs?

Regarding db error, looks like my attachment was not posted in approval process. Sending here
http://i68.tinypic.com/jaw5e9.jpg

Yes I ran the query directly in db and it worked fine. I know the records it returned is Huge and that is definitely the reason why error is coming. But issue is not the error, issue is how can I catch that error in the variable instead of just throwing the big CI standard error on screen?


RE: Error Handling with very basic SQL query - php_rocs - 04-22-2019

@aslamdoctor,

Maybe this will be helpful to you https://codeigniter.com/user_guide/database/queries.html?highlight=error#handling-errors
Also, I did find this link as well (a try catch statement) https://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter


RE: Error Handling with very basic SQL query - aslamdoctor - 04-22-2019

(04-22-2019, 01:16 AM)php_rocs Wrote: @aslamdoctor,

Maybe this will be helpful to you https://codeigniter.com/user_guide/database/queries.html?highlight=error#handling-errors
Also, I did find this link as well (a try catch statement) https://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter

If you check my code, I have already used similar code as per the user guide link you sent.
In try catch method, I can only put my own error text. But can not get the actual error in a variable or something like that.


RE: Error Handling with very basic SQL query - InsiteFX - 04-22-2019

See if this will work for you.

php.net - mssql_get_last_message

Identifying Critical Error Messages in MS SQL


RE: Error Handling with very basic SQL query - aslamdoctor - 04-22-2019

emm.. that function mssql_get_last_message is removed in php 7.0


RE: Error Handling with very basic SQL query - InsiteFX - 04-22-2019

Didn't see that sorry.