MSSQL+PHP = Issues
by z3n on Jul.21, 2009, under Coding
Problem:
On a simple MSSQL (Microsoft SQL Server) with PHP you get this error:
Warning: mssql_query() [function.mssql-query]: 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)
and the best, it dosen’t matter if you convert the data to a different type.P>
Solution:
It looks like that mssql library at php can’t handle the ntext objects right, or at least the way you’re calling it is not right, the ONG>wrong way is:
function _connect_mssql() {
global $_msql,$_msql_id;
if (!$_msql_id) {
if ($_msql=mssql_connect(dbserver,dbuser,dbpass)) {
mssql_select_db(db,$_msql) or die("Unable to select database: ".db);
$_msql_id=1;
} else {
die("Couldn’t connect to database");R> }
$_msql_id=1;
}
}
function _qm($sql) {
global $res,$_msql,$_msql_id;
if (!$_msql_id) { _connect_mssql(); }
$res=mssql_query($sql,$_msql);
if (!$res) { die("Error on query: ".$sql); }
}
Those are simple functions to call it without using a class or too much paraphernalia. And it doesn’t work when the table has a ntext column on it, now a function that works with any type of table:P>
function _connect_mssql() {
global $_msql,$_msql_id;
if (!$_msql_id) {
$_msql=new COM("ADODB.Connection");
$_msql->Open("Provider=SQLOLEDB.1;Password=".dbpass.";Persist Security Info=True;User ID=".dbuser.";Initial Catalog=".db.";Data Source=".dbserver);
$_msql_id=1;
}
}
function _qm($sql) {
global $res,$_msql,$_msql_id;
if (!$_msql_id) { _connect_mssql(); }
$res=$_msql->Execute($sql);
if (!$res) { die("Error on query: ".$sql); }
}
$_msql="";$_msql_id=0;
Note that I used a class and ADODB connection instead, this works prefectly.