sql-server 如何在Windows 7机器上连接MSSQL数据库和Zend Framework 3?

nbewdwxp  于 2022-10-31  发布在  Windows
关注(0)|答案(2)|浏览(139)

我正在尝试连接MSSQL数据库与Zend框架3.
但是我已经在我的Windows机器上将MSSQL与普通PHP代码连接起来了。为此我安装了sqlsrv。
下面是代码..

$serverName = "USER-PC\MYINSTANCE"; //serverName\instanceName
$connectionInfo = array( "Database"=>"MYSamples", "UID"=>"MYRawData", "PWD"=>"123456");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

请帮助我连接MSSQL数据库与Zend框架3。
先谢谢你

x7rlezfr

x7rlezfr1#

好吧,我终于有答案了。也许这个答案能帮助到一些人。
我在local.php(config\autoload\local.php)文件上做了以下更改

<?php
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;

return [
   'doctrine' => [
       'connection' => [
           'orm_default' => [
               'driverClass' => SQLSrvDriver::class,
               'params' => [
                   'host'     => 'USER-PC\MYINSTANCE',                    
                   'user'     => 'MYRawData',
                   'password' => '123456',
                   'dbname'   => 'MYSamples',
                   'port'     => '49166'
               ]
           ],            
       ],        
   ],
];

请参考此链接以查找mssql端口号:Identify Port used by SQL Server Database Engine Using SQL Server Configuration Manager

gojuced7

gojuced72#

必须安装“sqlsrv”PHP扩展。

Zend_Db::factory('sqlsrv', 
    array(
        'host'     => '<server_ip>',
        'username' => '<db_username>',
        'password' => '<db_password>',
        'dbname'   => '<db_name>',
        'charset'  => 'utf8'
    )
);

必须安装“dblib”pdo插件。

Zend_Db::factory('Pdo_mssql', 
    array(
        'pdoType'  => 'dblib',
        'host'     => '<server_ip>',
        'username' => '<db_username>',
        'password' => '<db_password>',
        'dbname'   => '<db_name>'
    )
);

要使用“dblib”,您需要安装https://www.freetds.org/userguide/

相关问题