How to connect to SQL Server database through PHP?

坚强是说给别人听的谎言 提交于 2019-12-12 02:27:23

问题


I'm building a web-service for an Android application that needs to connect to a SQL Server database. I'm trying to connect through PHP (WAMP) on my home computer to the SQL Server database.

However, I don't have experience SQL Server and don't know how to proceed. For SQL Server, I'm using default settings and Windows Authentication, so I'm not sure what to type into the connection string.

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.

Alternatively, how can I switch to a User-Name and Password Authentication in SQL Server?

LE: Using Microsoft SQL Server 2008 and SQL Server Management Studio


回答1:


PDO is the accepted way to connected to different databases in PHP. It also have a driver for MS-SQL

Here is an example (From PDO MSSQL)

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



回答2:


Microsoft made a driver for PHP, but I think it's better to go with PDO.

You can use PDO_SQLSRV:

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

Or use PDO_DBLIB (not available on Windows since PHP 5.3):

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



回答3:


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. – rdlowrey Feb 12 '12 at 14:58




回答4:


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



回答5:


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

mssql_connect($servername, $username, $password)

is how you connect to a Microsoft SQL database.



来源:https://stackoverflow.com/questions/9249690/how-to-connect-to-sql-server-database-through-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!