Sunday, 12 August 2012

PHP + MSSSQL issues

PHP + MSSQL unicode issues...

Last night, I was tinkering with using MSSQL with PHP.
All was going great.. created my database tables, created the stored procedures that were required for the project etc.

Now, the time to test out these stored procs was here. Slap a little php code together to connect to the database, and execute the stored procedure, when I was greeted with the following error message:

 Warning: mssql_execute(): message: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. (severity 16)  

Well.. this wasnt what I expected to see!

A bit of googling later, and the overwhelming suggestion was to cast my NTEXT fields as TEXT.
There's just one problem with this. None of my fields were NTEXT. (They were all NVARCHAR)

I gave it a whirl, modified my stored procs to cast to text, and retried. And the same error.Back to the drawing board. (in this case, more Googling).

A few more posts read later, FreeTDS 4.2 doesnt support unicode data. V8.0 does. So, lets change the version. Edit your freetds.conf file.. mine was located in

 /etc/freetds.conf  

I SFTP'ed the file down to my machine, and opened in text editor.

Mine looked a little like this:

 #  $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $  
 #  
 # This file is installed by FreeTDS if no file by the same   
 # name is found in the installation directory.   
 #  
 # For information about the layout of this file and its settings,   
 # see the freetds.conf manpage "man freetds.conf".   
 # Global settings are overridden by those in a database  
 # server specific section  
 [global]  
     # TDS protocol version  
 ;     tds version = 4.2  

Ah-ha! 4.2! - lets change that bad boy to 8.0.

 ;     tds version = 8.0  

Re-upload, and refresh my page...
..
..
And, still the same.

Thats when I noticed the semi-colon at the start. Now, me not being particularly up on linux, or tinkering with linux config files, I wasnt sure what the semi-colon meant.
As things turn out, its a comment.. so all text after it is ignored.  I knew that hashes (#) were comments, but didnt know about semi-colons. So what's the difference? Comments starting with # are a proper comment, and those starting with a ; character, indicate that this is a default value for an attribute.

Ah-ha! so, this is saying that the default value for the 'tds version' is 4.2.. I don't want to set a comment to show its default value, I want to set it.. simply remove the semi-colon, save, and re-upload.


 #  $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $  
 #  
 # This file is installed by FreeTDS if no file by the same   
 # name is found in the installation directory.   
 #  
 # For information about the layout of this file and its settings,   
 # see the freetds.conf manpage "man freetds.conf".   
 # Global settings are overridden by those in a database  
 # server specific section  
 [global]  
     # TDS protocol version  
      tds version = 8.0  

Refresh your page, and BINGO! proper text coming through from your stored procs :)



No comments:

Post a Comment