PHP 可以与 MS SQL 数据库一起使用吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5409027/
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
Can PHP work with a MS SQL database
提问by TH1981
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 preference.
我主要使用 PHP 和 MySQL,但我有一个具有 MS SQL 和 ASP 设置的潜在客户。由于一些复杂的原因和离线软件集成,他们需要将数据库保持在相同的格式,这意味着不要迁移到 MySQL,这将是我个人的偏好。
So the question is can I use PHP to access and manipulate an MS SQL database or am I screwed on this one?
所以问题是我可以使用 PHP 来访问和操作 MS SQL 数据库还是我搞砸了这个数据库?
Thanks in advance
提前致谢
回答by Craig
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 PDOfunctionality as well as the procedural style.
是的你可以。这取决于您使用的 PHP 版本,但如果您使用的是 PHP5+,则可以使用Microsoft 的 SQL Server Driver for PHP。确保您使用版本 2,它为您提供PDO功能以及程序样式。
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.
您还可以使用 PDO ODBC 驱动程序来访问 SQL Server 实例,但这种方法有更多问题,我不推荐它。
Finally you can use the PHP MSSQLlibrary but that's even worse. Go with Microsoft's own solution if you can.
最后,您可以使用 PHP MSSQL库,但这更糟。如果可以,请使用 Microsoft 自己的解决方案。
Edit: Oh, and there's also the DBLIB MSSQLPDO driver - stay away from that one too!
编辑:哦,还有DBLIB MSSQLPDO 驱动程序 - 也远离那个!
回答by psparrow
Yes. As long as you have the php_mssqlextension on your server, you can use the following common functions:
是的。只要您的服务器上有php_mssql扩展,您就可以使用以下常用功能:
// 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.
注意:从 PHP 5.3 开始,此扩展不包括在内(并且可能不维护)。您可以手动下载并添加它,或者更好地使用Microsoft 驱动程序。
回答by cdsaenz
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.
对我来说,解决方案是安装如上所述的 MS 驱动程序,并使用 ADODB 库作为中间。我已经通过 IIS6 和最新的 MSSQLExpress 在内部网中生产了这个,几个月没有任何问题,非常可靠。
回答by Muhammed Tanr?verdi
Also you can use sqlsrv if you are using PHP5.3+
如果您使用的是 PHP5.3+,也可以使用 sqlsrv
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 Drivers for PHP for SQL Server 3.1 版需要 PHP 5.4.32、PHP 5.5.16 或更高版本。Microsoft Drivers for PHP for SQL Server,版本 3.0 需要 PHP 5.3.0 或更高版本。如果可能,请使用 PHP 5.3.6 或更高版本。
Microsoft download page : microsoft sqlsrv download
Microsoft 下载页面:microsoft sqlsrv 下载
Microsoft SQL Server Driver for PHP Manual : php sqlsrv manual
适用于 PHP 的 Microsoft SQL Server 驱动程序手册:php sqlsrv 手册
回答by Ujjwal Manandhar
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
是的,您可以连接到 MsSQL 。如果您使用的是 wamp 则打开 php 扩展 php_mssql 如果没有则使用 php.ini 文件并修改它
回答by Shamit Verma
Yes, Microsoft provides a MS SQL driver for PHP.
是的,Microsoft 为 PHP 提供了 MS SQL 驱动程序。
Or you can access it via OBDC (Given the solution would be deployed on windows).
或者您可以通过 OBDC 访问它(鉴于该解决方案将部署在 Windows 上)。
http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx
http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx
回答by Delphy
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
是的,您可以同时使用 MS SQL 和 PHP。这是 PHP.net 中的一个页面,显示了所有函数和命令:MS SQL 和 PHP
It explains everything you need.
它解释了您需要的一切。
回答by mravey
You should take a look at those links : http://www.php.net/manual/en/ref.pdo-dblib.phpand http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
你应该看看这些链接:http: //www.php.net/manual/en/ref.pdo-dblib.php和 http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx