Part of the EllisLab Network
   
 
Support for error numbers, insert_id() and error strings in MSSQL driver
Posted: 11 February 2007 07:25 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  02-04-2007

Hi,

I fixed the issues in the subject line.  Note that the change to DB_driver.php is required so that the call to _error_number() does not screw up the _error_message() result.  Here is the patch:

Index: DB_driver.php
===================================================================
---
DB_driver.php       (revision 1)
+++
DB_driver.php       (revision 8)
@@ -
277,11 +277,13 @@

                        if (
$this->db_debug)
                        
{
-                               log_message('error', 'Query error: '.$this->_error_message());
+                               
$err_msg = $this->_error_message();
+                               
log_message('error', 'Query error: '.$err_msg);
+
                                return
$this->display_error(
                                                                                array(
                                                                                                
'Error Number: '.$this->_error_number(),
-                                                                                               
$this->_error_message(),
+                                                                                               
$err_msg,
                                                                                                
$sql
                                                                                        
)
                                                                                );
@@ -
1042,7 +1044,7 @@
                
$LANG->load('db');

                
$heading = 'MySQL Error';
-
+
                if (
$native == TRUE)
                
{
                        $message
= $error;
Index: drivers/mssql/mssql_driver.php
===================================================================
---
drivers/mssql/mssql_driver.php      (revision 1)
+++
drivers/mssql/mssql_driver.php      (revision 8)
@@ -
229,8 +229,10 @@
         */
        function
insert_id()
        
{
-               // Not supported in MS SQL?
-               return 0;
+               
$query = $this->query("SELECT @@IDENTITY AS insert_id");
+
+               
$row = $query->row();
+               return
$row->insert_id;
        
}

        
// --------------------------------------------------------------------
@@ -316,8 +318,10 @@
         */
        function
_error_message()
        
{
-               // Are errros even supported in MS SQL?
-               return '';
+               
// Note: only returns the last line of the last message.  If reporting an
+               // error, take care to fetch the message BEFORE the error number, as getting
+               // the error number involves performing a query.
+               return mssql_get_last_message();
        
}

        
// --------------------------------------------------------------------
@@ -330,8 +334,10 @@
         */
        function
_error_number()
        
{
-               // Are error numbers supported?
-               return '';
+               
$query = $this->query("SELECT @@ERROR AS error_number");
+
+               
$row = $query->row();
+               return
$row->error_number;
        
}

        
// --------------------------------------------------------------------
Profile
 
 
Posted: 22 May 2007 04:04 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  5
Joined  04-15-2007

Thanks for this, I just applied this patch and it works nicely…

Profile
 
 
Posted: 14 May 2009 05:46 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  7
Joined  07-28-2008

how on earth do you apply this patch?

Profile
 
 
Posted: 14 May 2009 12:44 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  60
Joined  01-18-2009

If using Linux or any other Unix-like OS, here is the way to apply the patch:
- Go to the system/database directory
- type this command: “patch -p1 -i path_to_file” (without the quotes)


No need to apply the patch because this code (or something similar) is already included in a current version of CodeIgniter.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77517 Total Logged-in Users: 27
Total Topics: 101527 Total Anonymous Users: 2
Total Replies: 544280 Total Guests: 257
Total Posts: 645807    
Members ( View Memberlist )