Error connecting to SQL Server.PDOException Object (Azure)

血红的双手。 提交于 2021-02-10 19:54:49

问题


i'm trying to connect to my Azure SQL db using PHP that is run by my other server every night at 00:00 via cronjob. When it runs shows this error in logs:

 Error connecting to SQL Server.PDOException Object
(
    [message:protected] => could not find driver
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /home3/nappuntx/public_html/modifiche/export.php
    [line:protected] => 73
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /home3/nappuntx/public_html/modifiche/export.php
                    [line] => 73
                    [function] => __construct
                    [class] => PDO
                    [type] => ->
                    [args] => Array
                        (
                            [0] => sqlsrv:server = tcp:prgsophie.database.windows.net,1433; Database = test_db
                            [1] => userTest
                            [2] => pswtest
                        )

                )

        )

    [previous:Exception:private] => 
    [errorInfo] => 
)
1

As mentioned here: https://docs.microsoft.com/it-it/sql/connect/php/microsoft-php-driver-for-sql-server?redirectedfrom=MSDN&view=sql-server-ver15 I Should install PDO Drivers in my Linux Server? I'm not sure because i've read here: https://www.php.net/manual/en/pdo.installation.php " PDO and the PDO_SQLITE driver is enabled by default as of PHP 5.1.0. You may need to enable the PDO driver for your database of choice; consult the documentation for database-specific PDO drivers to find out more about that. " so theoretically it is already included in my PHP driver. I use PHP Version 7.2.26 on my Linux Red Hat server... Waiting for support, thank you.


回答1:


Let me repeat your needs first. I see you were trying to use PHP PDO to connect Azure SQL Database, but the error information shows there is not a PHP driver to support SQL Azure connection, and your environment is as below.

  1. PHP 7.4, it seems that you installed v7.4 via compile it manually, please ensure you have installed it to /usr/bin or /usr/local/bin or other system path.
  2. Linux Server, but I don't know what Linux distribution you used, so I will assume that's Ubuntu/Debian.

Next first, you need to refer to the section PHP Version Support of the offical document Microsoft PHP Drivers for SQL Server Support Matrix to know what version of PHP PDO driver for Azure SQL Database is for PHP v7.4, as the figure below.

There seems to be not any version for PHP 7.4, but the latest version 5.6 is the only choice.

Then, you need to move to Download the Microsoft Drivers for PHP for SQL Server to click the link Microsoft Drivers 5.6 for PHP for SQL Server for Linux and macOS, it will redirect to its GitHub release page Production Release for the PHP drivers Version 5.6.0 for SQL Server and move to the page bottom, as the figure below.

Ofcourse, you can download the packages as you wish, but I think the simple way is to install sqlsrv and pdo_sqlsrv via pecl, and the first thing is to ready pecl via sudo apt install php-pear on Ubuntu/Debian Linux, then to do these below.

sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv

Now, I think you can try again for your PHP Script, may restart Apache/Nginx first if you used it.


Update: For using PHP 7.2 on RHEL, I recommended that please refer to the offical tutorial Create PHP apps using SQL Server on RHEL to know how to do step by step in details.



来源:https://stackoverflow.com/questions/59936735/error-connecting-to-sql-server-pdoexception-object-azure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!