PDO通过MSSQL_ *函数连接到MSSQL [英] PDO To Connect to MSSQL Over MSSQL_* Functions

查看:86
本文介绍了PDO通过MSSQL_ *函数连接到MSSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一系列 mssql _ * 不在折旧过程中。

There is a range of mssql_* Which are not in the depreciation process.

as mysql _ * functions;他们需要我手动转义,请找到以下手册的链接:

They work the same as mysql_* functions; they need to me manually escaped, please find the link to the manual below:

http://uk1.php.net/manual/en/book.mssql.php

MSSQL_ *功能分开 php5-mssql ,但现已移至 php5-sybase

MSSQL_* Functions was apart of php5-mssql but have now been moved into php5-sybase

此外,对于数据库构造使用PDO,但实验可用
http://php.net/manual/en/ref.pdo-dblib.php

Furthermore, using PDO for your Database Construct, is available but is experimental http://php.net/manual/en/ref.pdo-dblib.php

但我的整个问题,从事实上PDO / MySQLI正被推向主数据库通信解决方案,我应该停止使用 mssql _ *

But my overall question, from the fact that PDO/MySQLI is being pushed as main database communication solution, should I stop using the functions mssql_*

或者可以:

PDO连接:

$dsn = 'mssql:host=localhost;dbname=testdb';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

但是如果这个过程仍然被列为实验, $ c> Microsoft SQL Server 为其数据库,等待此扩展为稳定的MSSQL服务器

But if this process is still listed as experimental, should developers using Microsoft SQL Server for their databases, wait till this extension is stable for MSSQL Servers

一天结束,PDO扩展或MSSQL_ *函数即使它们没有折旧。如果是,为什么?

So at the end of the day, PDO Extension or MSSQL_* Functions even though they are not depreciated.. If so, why?

推荐答案

我自己的意见



我一直使用 PDO 连接到 MSSQL 数据库一年多了,到目前为止,我发现绝对没有问题。

My Own Opinion

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:


  1. 它非常轻便,可以用最少的代码切换到不同的数据库

  2. 它是安全的,不需要像 mysql_real_escape_string()

  3. 它正迅速成为开发者的标准

  4. 如果你没有经验面向对象编程,那么它是一个很好的介绍

  5. 它预装了大多数PHP包

  6. 它可以轻松执行comples查询,包括存储过程

  7. 使用MySQL数据库对旧版本的弃用的 mysql _ * 函数进行基准测试后,在很多情况下,如果不是所有情况。 - 见这里

  1. It is very portable, easy to switch to different databases with minimal code
  2. It is secure without the need of functions like mysql_real_escape_string()
  3. It is fast becoming the norm for developers
  4. If you do not have experience with Object Oriented Programming, then it is an excellent introduction
  5. It comes pre-installed with most PHP Packages
  6. It can execute comples queries with ease, including stored procedures
  7. After benchmarking it with a MySQL database against the old deprecated 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

这篇关于PDO通过MSSQL_ *函数连接到MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆