更新时间:2023-02-22 17:20:39
我一直使用 PDO
连接到 MSSQL
数据库一年多了,到目前为止,我发现绝对没有问题。
I have been using PDO
to connect to a MSSQL
database for over a year now and so far I have found absolutely no issues.
事实上,我研究使用 mssql_ *
函数,然后迁移到 PDO
,并得出结论,他们是一个不太可靠,更不用说,不安全的方式连接到 MSSQL
数据库。
In fact, I looked into using the mssql_*
functions before migrating to PDO
, and came to the conclusion that they were a much less reliable, not to mention, insecure way of connecting to a MSSQL
Database.
观点来看, PDO
也是更好的选择,因为它只需要对代码进行一些调整,以便从 MSSQL
到 MySQL
。
From a logical point of view, PDO
is also the better option as it only takes a few tweaks to the code in order to change from MSSQL
to MySQL
.
我为PDO类写了一个包装类,
I wrote a wrapper class for the PDO class that makes connecting to these databases very easy.
以此为例:
<?php
// +------------------------------------------------------------------------+
// | class.mssql.php |
// +------------------------------------------------------------------------+
// | Copyright (c) Company Ltd 2013. All rights reserved. |
// | Version 1.0 |
// | Last modified 30/01/2013 |
// | Email email@company.co.uk |
// | Web http://www.company.co.uk |
// +------------------------------------------------------------------------+
// Make sure the SQL class is included
require_once("class.sql.php");
/*
* Class mssql
*
* @version 1.0
* @author Ben Carey <email@company.co.uk>
* @copyright Company Ltd
*
*/
class mssql extends sql{
/**
* Initialize the object and set/reset all variables
*
* This function is called when the object is constructed
*
* @access private
*/
function __construct(&$memcache){
// Call the sql construct
parent::__construct($memcache);
// Global MsSQL defaults
$this->query_escaper_left = "[";
$this->query_escaper_right = "]";
$this->connection_engine = "sqlsrv";
$this->connection_parameter_host = "server";
$this->connection_parameter_database = "Database";
$this->select_db_function = "db_name()";
}
}
?>
定义任何 MSSQL
在此扩展中,然后传递到父类 class.sql.php
。 PDO的优点是文件 class.sql.php
中的代码不必以任何方式改变以使用任何数据库(或所有数据库我已经尝试了这么远)。
Anything that is unique to MSSQL
is defined in this extension and then passed up to the parent class class.sql.php
. The beauty of PDO is that the code in the file class.sql.php
does not have to be altered in any way to work with any database (or, all the databases that I have tried thus far).
所以这里需要的是每个数据库类型的小扩展,它将工作。
So all that is needed here is a small extension for each database type and it will work.
然而,对于原生的 mssql _ *
函数,如果你决定因任何特殊原因更改数据库,你必须重写所有的东西。更何况,鉴于 mysql _ *
函数现在已被弃用,您将不得不使用PDO for MySQL。
Whereas, with the native mssql_*
functions, if you were to decide to change database for any particular reason, you would have to rewrite everything. Not to mention, you would have to use PDO for MySQL anyway given that the mysql_*
functions are now deprecated.
我已经运行复杂的存储过程,输入参数
, OUTPUT PARAMETERS
, INOUT PARAMETERS
,其中包含100,000,000条以上的记录。
I have been running complex stored procedures, with INPUT PARAMETERS
, OUTPUT PARAMETERS
, INOUT PARAMETERS
, on databases with 100,000,000+ records in them. These have worked absolutely flawlessly, and continue to do so!
另一个原因是不使用 mssql _ *
函数是在PHP 5.3或更高版本的Windows上不再支持的:
Another reason not to use the mssql_*
functions is that they are no longer supported on Windows with PHP version 5.3 or later:
a href =http://uk1.php.net/manual/en/intro.mssql.php>请参阅这里
See Here
SyBase扩展与 mssql _ *
函数属于同一类别。
The SyBase Extension falls under the same category as the mssql_*
functions. They are procedural, impractical and not portable at all!
一目了然,我注意到这些扩展都没有等价于 mysql_real_escape_string()
函数的函数。而在PDO中,没有必要这个
At a glance, I have noticed that none of these extensions have a function equivalent to the mysql_real_escape_string()
function. Whereas, in PDO, there is no need for this
不用说我是道德的PDO支持者(这只有来使用它1年后!)。这不是说我不会听其他人对 mssql _ *
功能的意见,它很难说服我,我想大多数人,这些
It goes without saying that I am a moral PDO supporter (and this has only come after using it for 1 year!). That is not to say I will not listen to other peoples opinions on the mssql_*
functions, it will just be very hard to persuade me, and I think most people, that these functions can even compete the PDO.
总而言之,我认为PDO是向前的方向有以下几个主要原因:
So to conclude, in my opinion, PDO is the way forward for the following key reasons:
mysql_real_escape_string()
mysql _ *
函数进行基准测试后,在很多情况下,如果不是所有情况。 - 见这里 mysql_real_escape_string()
mysql_*
functions, it has proved to be faster in a lot of cases, if not all cases. - See Here
我问过一个类似的问题,也得出了相同的结论:
I asked a similar question a while back, and the same conclusion was drawn:
See here