The php program is UTF-8, sqlserver is 2005 Chinese characters, and the internal code is gb18030. the common mssql_connect cannot set the internal code conversion. the read data is garbled on the UTF-8 page. Zhou Haihan/Wen
ADO can use statements such as new COM ("ADODB. Connection", NULL, CP_UTF8) // 65001 to achieve correct conversion. However, ADO's support for php lacks documentation. There is an open-source adodb with rich documentation.
For different database drivers, the way to set the UTF-8 is also different, as follows:
The code is as follows:
For all drivers
'Persistent', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'Did', 'charset', 'buffers', 'role'
M'soft ADO
'Charpage'
MySQL
'Clientflags'
MySQLi
'Port', 'socket ', 'clientflags'
Oci8
'Nls _ date_format ', 'charset'
For all drivers
'Persistent', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'Did', 'charset', 'buffers', 'role'
M'soft ADO
'Charpage'
MySQL
'Clientflags'
MySQLi
'Port', 'socket ', 'clientflags'
Oci8
'Nls _ date_format ', 'charset'
Where Ado can use the charPage property to set the uft-8, similar to the new COM method. However, it is found that when the $ dbdriver of AdoNewConnection ($ dbdriver) is set to 'ado 'or 'ado _ mssql', the database to be passed in is replaced with provider. How can I set the database name? No way has been found.
$ Dbdriver = 'ado: // sa: cvttdev@172.16.22.40/sqloledb? Charpage = 65001 ';
The format is 'driver: // user: passwd @ host/database? Options [= value]
But it does not solve the problem of setting the database name.
After a long time, you can only find the following solution:
The code is as follows:
$ Dbdriver = 'ado _ mssql ';
$ Server = '1970. 168.22.40 ';
$ User = 'sa ';
$ Password = 'passwd ';
$ DATABASE = 'sugarcrm _ db ';
$ Database = 'sqloledb ';
// $ Dbdriver = 'ado: // sa: cvttdev@172.16.22.40/sqloledb? Charpage = 65001 ';
$ MyDSN = "PROVIDER = MSDASQL; DRIVER = {SQL Server}; SERVER = {172.16.22.40}; DATABASE = sugarcrm_db; UID = sa; PWD = cvttdev ;";
Include ('adodb5/adodb. inc. php ');
$ Db = ADONewConnection ($ dbdriver); # eg 'mysql' or 'mongos'
$ Db-> debug = true;
$ Db-> charPage = 65001;
// $ Db-> Connect ($ server, $ user, $ password, $ database );
$ Db-> Connect ($ myDSN );
// Error: mssql server not support codes below
// $ Db-> Execute ("set names 'utf8 '");
Echo "before query ";
$ Rs = $ db-> Execute ('select * from accounts ');
Print"
";
print_r($rs->GetRows());
print "
";
?>