1. Installation Configuration FreeTDS
wget http://tar zxf freetds_0. the . orig.tar.gz cd freetds_0. the . /configure--prefix=/usr/local/freetds--with-tdsver=8.0 -–enable-msdblib-–enable-dbmfix-–with-gnu-ld- enable-shared-–enable-Static
2. Compiling the MSSQL module of PHP
cd/path/to/php/source enters the PHP source directory CD ext//usr/local/webserver/php/bin/phpize generates the compilation configuration file. /configure–with-php-config=/usr/local/webserver/php/bin/php-config–with-mssql=/usr/local/
Compile to build mssql.so, modify php.ini, load the module into:
extension= "/path/to/extension/mssql.so"
3. Configure MSSQL
cd/usr/local/freetds/etc Edit file: vi freetds.conf [Global] # TDS protocol version; TDS version=4.2# Whether to write a tdsdump file forDiagnostic purposes # (setting ThisTo/tmp isInsecure on a multi-user system); Dump file=/tmp/freetds.log; Debug Flags=0xFFFF# Command and connection timeouts; timeout=Ten; Connect Timeout=Ten# If YouGet out-of-memory errors, it may mean that your client # isTrying to allocate a huge buffer fora text field. # Try setting ' text Size'To a more reasonable limitText size =64512Client CharSet= utf-8#加入 #加入 [Server2005] host=192.168. x.x Port=1433TDS Version=7.2
4. Test TDS
./tsql-s 192.168.0.109-u User name-p password-d database
If you can see 1> the connection is successful
5. Test PHP Connection MSSQL
Copy CodeThe code is as follows:
<?php
try {
$hostname = ' 218.x.x.x ';//note, this is different from the above, to use the IP address or host name directly
$port =1433;//Port
$dbname = "user";//Library name
$username = "database";//user
$PW = "passwd";//Password
$DBH = new PDO ("dblib:host= $hostname: $port;d bname= $dbname", "$username", "$PW");
} catch (Pdoexception $e) {
echo "Failed to get DB handle:". $e->getmessage (). " n ";
Exit
}
Echo ' connent MSSQL succeed ';
$stmt = $dbh->prepare ("SELECT * from Z_2010pinjiu_user");
$stmt->execute ();
while ($row = $stmt->fetch ()) {
Print_r ($row);
}
Unset ($DBH); Unset ($stmt);
?>