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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!