php 如何使用PHP连接sql server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18632607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PHP to connect to sql server
提问by maha
I want to use PHP to connect to sql server database.
我想使用 PHP 连接到 sql server 数据库。
I installed xampp 1.7.0(php 5.2) and SQLSRV20. I've added the extensions in php.ini
and I get this error:
我安装了 xampp 1.7.0(php 5.2) 和 SQLSRV20。我已经添加了扩展,php.ini
但出现此错误:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to
server: 10.85.80.229 in C:\xampp\htdocs\xampp\test.php on line 07
Code:
代码:
<?php
$myServer = "10.85.80.229";
$myUser = "root";
$myPass = "pass";
$myDB = "testdb";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
What does this error message mean and how do I connect to SQL Server?
此错误消息是什么意思,如何连接到 SQL Server?
回答by Agha Umair Ahmed
enable mssql in php.ini
在 php.ini 中启用 mssql
;extension=php_mssql.dll
;extension=php_mssql.dll
to
到
extension=php_mssql.dll
extension=php_mssql.dll
回答by Harikris
<?php
$serverName = "ServerName";
$uid = "sqlusername";
$pwd = "sqlpassword";
$databaseName = "DBName";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT id, FirstName, LastName, Email FROM tblContact";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.<br>\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "Col1: ".$row[0]."\n";
echo "Col2: ".$row[1]."\n";
echo "Col3: ".$row[2]."<br>\n";
echo "-----------------<br>\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
http://robsphp.blogspot.ae/2012/09/how-to-install-microsofts-sql-server.html
http://robsphp.blogspot.ae/2012/09/how-to-install-microsofts-sql-server.html
回答by Guru
Try this code
试试这个代码
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
回答by user3389579
if your using sqlsrv_connect you have to download and install MS sql driver for your php. download it here http://www.microsoft.com/en-us/download/details.aspx?id=20098extract it to your php folder or ext in xampp folder then add this on the end of the line in your php.ini file
如果您使用 sqlsrv_connect,则必须为您的 php 下载并安装 MS sql 驱动程序。在此处下载http://www.microsoft.com/en-us/download/details.aspx?id=20098将其解 压缩到您的 php 文件夹或 xampp 文件夹中的 ext,然后将其添加到您的 php.ini 文件的行尾。 ini文件
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
im using xampp version 5.5 so its name php_pdo_sqlsrv_55_ts.dll & php_sqlsrv_55_ts.dll
我使用的是 xampp 5.5 版,所以它的名字是 php_pdo_sqlsrv_55_ts.dll & php_sqlsrv_55_ts.dll
if you are using xampp version 5.5 dll files is not included in the link...hope it helps
如果您使用的是 xampp 5.5 版 dll 文件,则链接中不包含...希望对您有所帮助
回答by hassan javaid
$server_name = "your server name";
$database_name = "your database name";
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$database_name;ConnectionPooling=0", "user_name", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
回答by nimesh
$dbhandle = sqlsrv_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
Hope it help.
希望有帮助。
回答by Manish Chauhan
Use localhost
instead of your IP address.
使用localhost
代替您的 IP 地址。
e.g,
例如,
$myServer = "localhost";
And also double check your mysql username and password.
还要仔细检查您的 mysql 用户名和密码。
回答by Pascal Fischer
for further investigation: print out the mssql error message:
进一步调查:打印出 mssql 错误消息:
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
It is also important to specify the port: On MS SQL Server 2000, separate it with a comma:
指定端口也很重要:在 MS SQL Server 2000 上,用逗号分隔:
$myServer = "10.85.80.229:1443";
or
或者
$myServer = "10.85.80.229,1443";
回答by chirag ode
For the following code you have to enable mssql in the php.ini as described at this link: http://www.php.net/manual/en/mssql.installation.php
对于以下代码,您必须按照此链接所述在 php.ini 中启用 mssql:http: //www.php.net/manual/en/mssql.installation.php
$myServer = "10.85.80.229";
$myUser = "root";
$myPass = "pass";
$myDB = "testdb";
$conn = mssql_connect($myServer,$myUser,$myPass);
if (!$conn)
{
die('Not connected : ' . mssql_get_last_message());
}
$db_selected = mssql_select_db($myDB, $conn);
if (!$db_selected)
{
die ('Can\'t use db : ' . mssql_get_last_message());
}
回答by Dean Anderson
I've been having the same problem (well I hope the same). Anyways it turned out to be my version of ntwdblib.dll, which was out of date in my PHP folder.
我一直有同样的问题(我希望也是如此)。无论如何,结果是我的 ntwdblib.dll 版本,它在我的 PHP 文件夹中已过时。
http://dba.fyicenter.com/faq/sql_server_2/Finding_ntwdblib_dll_Version_2000_80_194_0.html
http://dba.fyicenter.com/faq/sql_server_2/Finding_ntwdblib_dll_Version_2000_80_194_0.html