dqlk31541 2017-10-12 18:06
浏览 160
已采纳

如何在AWS Amazon Linux EC2实例上从PHP 7连接到SQL Server实例?

I have Apache 2.4 and PHP 7 installed and working fine on an Amazon Linux AMI on AWS. From PHP, I can connect to MySQL DBs just fine, but now I need to connect to a SQL Server DB.

How do I do this? What's the PHP code I need to write and do I need to install additional PHP packages/modules to do this? I already have PDO installed and tried to use it, but the following code throws the following error:

$dsn = 'sqlsrv:dbname=db-name;host=aws-endpoint.rds.amazonaws.com';
$user = 'user-name';
$password = 'password';
$dbh = new PDO($dsn, $user, $password);

Error:

PHP Fatal error:  Uncaught PDOException: could not find driver in /var/www/html/php-script-name.php

Edit: Adding to my question a bit, I ran a yum search pdo from the EC2 instance and got the following:

Loaded plugins: priorities, update-motd, upgrade-helper
============================================================================================================= N/S matched: pdo ==============================================================================================================
php-ZendFramework-Db-Adapter-Pdo.noarch : Zend Framework database adapter for PDO
php-ZendFramework-Db-Adapter-Pdo-Mssql.noarch : Zend Framework database adapter for MS SQL PDO
php-ZendFramework-Db-Adapter-Pdo-Mysql.noarch : Zend Framework database adapter for MySQL PDO
php-ZendFramework-Db-Adapter-Pdo-Pgsql.noarch : Zend Framework database adapter for PgSQL PDO
php70-pdo-dblib.x86_64 : PDO driver Microsoft SQL Server and Sybase databases
php71-pdo-dblib.x86_64 : PDO driver Microsoft SQL Server and Sybase databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php54-pdo.x86_64 : A database access abstraction module for PHP applications
php55-pdo.x86_64 : A database access abstraction module for PHP applications
php56-pdo.x86_64 : A database access abstraction module for PHP applications
php70-pdo.x86_64 : A database access abstraction module for PHP applications
php71-pdo.x86_64 : A database access abstraction module for PHP applications

  Name and summary matches only, use "search all" for everything.

It seems like php70-pdo-dblib.x86_64 is the plugin I want, so I ran yum install on it and my phpinfo now looks like the following:

enter image description here

I did restart Apache as well, but I still get the error noted above with the code noted above.

As such, I do not think that this is a duplicate of the question linked in the comments section.

  • 写回答

3条回答 默认 最新

  • dpoh61610 2017-10-12 18:46
    关注

    I figured it out. You do in fact have to have php70-pdo-dblib installed and restart the server. However, my $dbn string was off. Specifically, sqlsrv needed to be changed to dblib and I added the port number :1433 onto the end of the host part.

    As such, the following code worked:

    $dsn = 'dblib:host=aws-endpoint.rds.amazonaws.com:1433;dbname=db-name;';
    $user = 'user-name';
    $password = 'password';
    
    try {
    
      $dbh = new PDO($dsn, $user, $password);
    
      $result = $dbh->query("SELECT * 
        FROM table-name;");
    
      foreach ($result as $row) {
        echo '<pre>';
          print_r($row);
        echo '</pre>';
      }
    
    } catch (PDOException $e) {
    
      echo 'Connection failed: ' . $e->getMessage();
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python ModuleNotFoundError报错,如何解决?
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变