php链接sqlserver四种方法分别是mssql、pdo-mssql、sqlsrv-pdo、sqlsrv
对应php.ini的以下4个,去掉前面的分号注释后可能需要相关的驱动文件。
以下是具体的测试代码:KJDSC是数据库,JC_MESSAGE是具体的表。推荐使用 sqlsrv-pdo 高版本的php也可以很好的支持。 //mssql $rs= mssql_connect('127.0.0.1', 'sa', 'root'); mssql_select_db("KJDSC"); $sql = 'select * from JC_MESSAGE'; $result = mssql_query($sql); $row = mssql_fetch_array($result); print_r($row); echo "<hr /><br/><br/><br/>"; //pdo-mssql $dsn = 'mssql:dbname=KJDSC;host=127.0.0.1'; $user = 'sa'; $password = 'root'; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } $sql = 'select * from JC_MESSAGE'; $sth = $dbh->query($sql); $result = $sth->fetchAll(); print_r($result); echo "<hr /><br/><br/><br/>"; //sqlsrv-pdo try{ $dbh = new PDO("sqlsrv:Server=127.0.0.1,1433;Database=KJDSC","sa","root"); }catch(PDOException $e){ echo "ERROR:".$e->getMessage(); } if($dbh)echo "OK!Connected!<br />"; $sql = 'select * from JC_MESSAGE'; $sth = $dbh->query($sql); $result = $sth->fetchAll(); print_r($result); echo "<hr /><br/><br/><br/>"; //sqlsrv $serverName = "127.0.0.1"; $connectionInfo = array( "UID"=>"sa", "PWD"=>"root", "Database"=>"KJDSC"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.\n"; $sql = 'select * from JC_MESSAGE'; $result = sqlsrv_query($conn,$sql); $row = sqlsrv_fetch_array($result); print_r($row); echo "<hr /><br/><br/><br/>"; } else { echo "Connection could not be established.\n"; die( print_r( sqlsrv_errors(), true)); }