Zend Framework 2 Sql Service(MSSQL)数据库链接,其它架构方法类似

检查是否安装成功:
在phpinfo可查看(sqlsrv) PDO drivers: sqlsrv, mysql, odbc, sqlite)
SQL Server2008需下载Microsoft® ODBC Driver 11 for SQL Server (SQL Server2012)

http://www.microsoft.com/zh-cn/download/details.aspx?id=36434

Step 1:

下载 Microsoft Drivers 3.0 for PHP for SQL Server  驱动,3.0版本的,这个安装包是几个dll文件打包,安装到php/ext/目录下


版本3.2和3.1需要Microsoft ODBC驱动程序11(或更高版本)的SQL Server。要下载Microsoft ODBC驱动程序11 PHP的SQL Server,请参阅SQL Server的Microsoft ODBC驱动程序11。

如果您使用的是SQLSRV驱动程序,sqlsrv_client_info将返回有关哪个版本的SQL Server本机客户端的正在使用的PHP微软驱动程序SQL Server的信息。如果您使用的是PDO_SQLSRV驱动程序,您可以使用PDO ::的getAttribute发现的版本。

对于版本3.0和2.0,SQL Server本机客户端的x86版本是必需的,其中PHP运行在计算机上。如果您使用的是64位操作系统,SQL Server本机客户端的x86版本将与SQL Server Native Client的x64版本安装(不要安装SQL Server本机客户端上的64位操作系统的x86版本)。

如果您使用的是SQLSRV驱动程序,sqlsrv_client_info将返回有关哪个版本的SQL Server本机客户端的正在使用的PHP微软驱动程序SQL Server的信息。如果您使用的是PDO_SQLSRV驱动程序,您可以使用PDO ::的getAttribute发现的版本。

    驱动程序3.0版需要Microsoft SQL Server 2012的本机客户端。您可以从SQL Server 2012的功能包页面下载Microsoft SQL Server 2012的本机客户端。

  • Versions 3.2 and 3.1 require Microsoft ODBC Driver 11 (or higher) for SQL Server. To download the Microsoft ODBC Driver 11 for PHP for SQL Server, seeMicrosoft ODBC Driver 11 for SQL Server.

    If you are using the SQLSRV driver, sqlsrv_client_info will return information about which version of SQL Server Native Client is being used by the Microsoft Drivers for PHP for SQL Server. If you are using the PDO_SQLSRV driver, you can usePDO::getAttribute to discover the version.

  • For versions 3.0 and 2.0, the x86 version of SQL Server Native Client is required on the computer where PHP is running. If you are using a 64-bit operating system, the x86 version of SQL Server Native Client will be installed with the x64 version of SQL Server Native Client (do not install the x86 version of SQL Server Native Client on an x64 operating system).

    If you are using the SQLSRV driver, sqlsrv_client_info will return information about which version of SQL Server Native Client is being used by the Microsoft Drivers for PHP for SQL Server. If you are using the PDO_SQLSRV driver, you can usePDO::getAttribute to discover the version.





php_pdo_sqlsrv_53_nts.dll
php_pdo_sqlsrv_53_ts.dll
php_pdo_sqlsrv_54_nts.dll
php_pdo_sqlsrv_54_ts.dll  
(经测试可用 extension=php_pdo_sqlsrv_54_ts.dll ,安装后在phpinfo可查看(sqlsrv) PDO drivers: sqlsrv, mysql, odbc, sqlite)
php_sqlsrv_53_nts.dll
php_sqlsrv_53_ts.dll
php_sqlsrv_54_nts.dll
php_sqlsrv_54_ts.dll

根据自己的php版本以及需求选择合适的文件,在php.ini的extension中加入扩展(比如我的就为extension=php_sqlsrv_54_ts.dll)注意:别忘记重启服务器

Step 2:

安装Microsoft® SQL Server® 2012 Native Client. 进入链接后点开安装说明,在MICROSOFT SQL SERVER 连接性功能包组件下就能看见
Microsoft® SQL Server® 2012 Native Client选择合适的包下载并安装。

Step 3:

配置ZF2的全局配置文件 ( path: /config/autoload/global.php )

连接 ==== My Sql =====

//全局环境变量 ,全局数组在这里配置
return array(
    /*My Sql */
    'db'=>array(
        'driver'=>'Pdo',    //如果为Mysqli,则其它键名称不同
        'dsn'=>'mysql:dbname=lf_xxx;host=localhost',
    //     'username'=>'root',
    //    'password'=>'xxxx',        
        'driver_options'=>array(
            PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'',
        ),
        'prefix'=>'lf_',
    ),
       
    'service_manager'=>array(
        'factories'=>array(
            'Zend\Db\Adapter\Adapter'=>'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
}


Ms SQL Server 方法一

/*Sql Server 测试 Success*/
    'db' => array(
        'driver' => 'Pdo',          //只要包含Pdo前缀,系统会自动识别
        //'platform' => 'SqlServer',
        'dsn' => 'sqlsrv:Database=sql_xxx_co_uk;server=localhost',
        //'username' => 'sa',       //注意修改local.php
        //'password' => 'xxxx',
    ),

方法二 sqlServer 待测
<?php
return array(
   'db' => array(
        'driver'   => 'Sqlsrv',
        'server'   => 'Star-PC',   //数据库服务器名,一般来说就是你的电脑名
        'Database' => 'zf2_test',  //数据库名称
        'USER'     => 'sa',        //数据库用户名
        'password' => ''           //数据库密码
    ),
    //数据库适配器
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);


Yii2配置 config/db.php
return [
    'class' => 'yii\db\Connection',
    //'dsn' => 'mssql:host=localhost;dbname=shop_obd2', // MySQL, MariaDB
    //'dsn' => 'sqlite:/path/to/database/file', // SQLite
    //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL
    //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID
    'dsn' => 'sqlsrv:Server=localhost\mssql2008;Database=Sql_xxxx', // MS SQL Server, sqlsrv driver
    //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver
    //'dsn' => 'mssql:host=localhost;dbname=Sql_xxxx', // MS SQL Server, mssql driver
    //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
        
    'username' => 'sa',
    'password' => 'xxxx',
    'charset' => 'utf8',
    //'tablePrefix' => 'tb_',
];


=== PHP 连接 Mssql2008 ===

php 5.4 and MS SQL 2008 connection and it works fine, the following connection was established successfully.

/*
$serverName = "testserver\test"; //serverName\instanceName
$connectionInfo = array( "Database"=>"payroll", "UID"=>"sa", "PWD"=>"123456");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
    echo "---------- Connection established --------------------.<br />";     
    $sql = "select * from users";
    $stmt = sqlsrv_query($conn, $sql);
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
          echo $row['id'].", ".$row['username']."<br />";
    }    
} else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值