• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Handling with very basic SQL query

#1
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);

Reply

#2
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'

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(04-21-2019, 10: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.
Reply

#4
@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?
Reply

#5
(04-21-2019, 12:08 PM)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?
Reply

#6
@aslamdoctor,

Maybe this will be helpful to you https://codeigniter.com/user_guide/datab...ing-errors
Also, I did find this link as well (a try catch statement) https://stackoverflow.com/questions/3418...deingniter
Reply

#7
(04-22-2019, 02:16 AM)php_rocs Wrote: @aslamdoctor,

Maybe this will be helpful to you https://codeigniter.com/user_guide/datab...ing-errors
Also, I did find this link as well (a try catch statement) https://stackoverflow.com/questions/3418...deingniter

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.
Reply

#8
See if this will work for you.

php.net - mssql_get_last_message

Identifying Critical Error Messages in MS SQL
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
emm.. that function mssql_get_last_message is removed in php 7.0
Reply

#10
Didn't see that sorry.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.