I work primarly with PHP & MySQL, but I have a potential client with a MS SQL and ASP setup. Due to some complicated reasons and offline software integration, they need to keep the databases in the same format, which means not moving to MySQL which would be my personal 开发者_StackOverflowpreference.
So the question is can I use PHP to access and manipulate an MS SQL database or am I screwed on this one?
Thanks in advance
Yes, you can. It depends on which version of PHP you're using, but if you're using PHP5+ you can use Microsoft's SQL Server Driver for PHP. Make sure you use version 2, which gives you the PDO functionality as well as the procedural style.
You can also use the PDO ODBC driver to access a SQL Server instance, but that approach is more buggy and I don't recommend it.
Finally you can use the PHP MSSQL library but that's even worse. Go with Microsoft's own solution if you can.
Edit: Oh, and there's also the DBLIB MSSQL PDO driver - stay away from that one too!
Yes. As long as you have the php_mssql extension on your server, you can use the following common functions:
// Connect to mssql server
$handle = mssql_connect($host, $user, $pass) or die("Cannot connect to server");
// Select a database
$db = mssql_select_db($dn_name, $handle) or die("Cannot select database");
// Execute a query
$query = "SELECT * FROM users WHERE lname = 'Smith'";
$result = mssql_query($query);
// Iterate over results<br />
while($row = mssql_fetch_array($result)) {
echo $row["id"];
}
Note: From PHP 5.3 this extension is not included (and probably not maintained). You can download and add it manually, or better use Microsoft drivers.
yes you can connect to MsSQL . If you are using wamp then switch on the php extension php_mssql if not then use the php.ini file and modify it
Yes, Microsoft provides a MS SQL driver for PHP.
Or you can access it via OBDC (Given the solution would be deployed on windows).
http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx
Yes, you can use MS SQL and PHP together. Here is just a page from the PHP.net showing all the functions and commands: MS SQL and PHP
It explains everything you need.
You should take a look at those links : http://www.php.net/manual/en/ref.pdo-dblib.php and http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
For me the solution has been to install the MS drivers as indicated above, and use ADODB library as intermediate. I've had this in production in an intranet over IIS6 and latest MSSQLExpress for months without any issue, perfectly reliable.
Also you can use sqlsrv if you are using PHP5.3+
Form microsoft
Microsoft Drivers for PHP for SQL Server, version 3.1 requires PHP 5.4.32, or PHP 5.5.16, or later. Microsoft Drivers for PHP for SQL Server, version 3.0 requires PHP 5.3.0 or later. If possible, use PHP 5.3.6, or later.
Microsoft download page : microsoft sqlsrv download
Microsoft SQL Server Driver for PHP Manual : php sqlsrv manual
精彩评论