简体   繁体   English

如何通过PHP连接SQL Server数据库?

[英]How to connect to SQL Server database through PHP?

I'm building a web-service for an Android application that needs to connect to a SQL Server database.我正在为需要连接到 SQL Server 数据库的 Android 应用程序构建 Web 服务。 I'm trying to connect through PHP (WAMP) on my home computer to the SQL Server database.我正在尝试通过家用计算机上的 PHP (WAMP) 连接到 SQL Server 数据库。

However, I don't have experience SQL Server and don't know how to proceed.但是,我没有 SQL Server 的经验,不知道如何继续。 For SQL Server, I'm using default settings and Windows Authentication, so I'm not sure what to type into the connection string.对于 SQL Server,我使用的是默认设置和 Windows 身份验证,因此我不确定在连接字符串中输入什么内容。

Below you have my connection:下面是我的连接:

$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
$conn->open($connStr); //Open the connection to the database

I haven't found a concrete example anywhere so far, so I need to know what the variables $myServer , $myUser etc. need to be in the case of Windows Authentication.到目前为止,我还没有在任何地方找到具体的例子,所以我需要知道在 Windows 身份验证的情况下$myServer$myUser等变量需要是什么。

Alternatively, how can I switch to a User-Name and Password Authentication in SQL Server?或者,如何在 SQL Server 中切换到用户名和密码身份验证?

LE: Using Microsoft SQL Server 2008 and SQL Server Management Studio LE:使用 Microsoft SQL Server 2008 和 SQL Server Management Studio

PDO is the accepted way to connected to different databases in PHP. PDO是在 PHP 中连接到不同数据库的公认方式。 It also have a driver for MS-SQL它还有一个用于 MS-SQL驱动程序

Here is an example ( From PDO MSSQL )这是一个示例(来自 PDO MSSQL

$con = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");

Microsoft made a driver for PHP , but I think it's better to go with PDO.微软为 PHP制作了一个驱动程序,但我认为最好使用 PDO。

You can use PDO_SQLSRV:您可以使用 PDO_SQLSRV:

$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);

Or use PDO_DBLIB (not available on Windows since PHP 5.3):或者使用 PDO_DBLIB(自 PHP 5.3 起在 Windows 上不可用):

$dbh = new PDO("dblib:host=$hostdb;dbname=$dbname", $usr, $psw);       

In a related note, the same fate awaits the mysql* extensions if people will ever stop insisting on using them (and the terrible "tutorial" sites stop advocating them) instead of the vastly superior PDO extension.在相关说明中,如果人们不再坚持使用 mysql* 扩展(并且糟糕的“教程”站点不再提倡它们),而不是非常出色的 PDO 扩展,那么同样的命运等待着 mysql* 扩展。 – rdlowrey Feb 12 '12 at 14:58 – rdlowrey 2012 年 2 月 12 日 14:58

$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);

According to: http://www.php.net/manual/en/function.mssql-connect.php ,根据: http://www.php.net/manual/en/function.mssql-connect.php

mssql_connect($servername, $username, $password)

is how you connect to a Microsoft SQL database.是连接到 Microsoft SQL 数据库的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM