2

I want to run Microsoft Access Database using PDO_ODBC (on Centos 7 x64 bit). But unfortunately i get this error on the page :

could not find driver

First of all i am looking the problem through my connection.php, but seems like there is no problem in my code (tested on Windows 7 x64 bit).

And then i think the driver has not enabled yet, i look into phpinfo() page and yes the driver has not enabled yet.

And then i go into php.ini to enable the driver. I uncomment the following line in php.ini:

extension=pdo_odbc

And then i restart the apache using this command:

# /etc/init.d/apache2 restart

After that i look into phpinfo() page, unfortunately the setting that i have set has not enabled yet, this is the screenshot for the phpinfo(): phpinfo() page.

Would you tell me how to enable PDO_ODBC on Linux (CentOS 7)?

UPDATE TO SHOW MY CONNECTION

connection.php:

<?php 

    $dsn = "{Microsoft Access Driver (*.mdb, *.accdb)}";
    $db = "/home/www/html/cetak_absensi/uploaded/db_access/my_access_db.mdb";
    $user = "";
    $pass = "";

    if (!file_exists($db)) {
        die("File is not exists ! " . $db);
    }else{
        try {
            $koneksi = new PDO("odbc:DRIVER=".$dsn.";charset=UTF-8; Dbq=".$db."; Uid=".$user."; Pwd=".$pass.";");
            $koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "There is an error : <i>" . $e->getMessage() . "</i>";
            die();
        }
    }
  • 1
    **Driver** and **extension** are two different things. Also check that you edited the *correct* `php.ini`, for your webserver, and not the one for the cli sapi. https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 – Daniel W. May 11 '20 at 16:11
  • @DanielW. : I ran this command to know where the php.ini is located : `# php -i | grep "Loaded Configuration File"`. And the output is `Loaded Configuration File => /etc/php/7.2/cli/php.ini`. I changed the setting of _php.ini_ that placed on the output. – Dont Bullying Me May 11 '20 at 16:44
  • @DanielW. : I am new on Linux, i don't know exactly how to do what the docs said. I ran `curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo` (Like the docs said) in the terminal, then it said `-bash: /etc/yum.repos.d/mssql-release.repo: No such file or directory`. – Dont Bullying Me May 11 '20 at 16:59
  • I have restore php.ini on cli to the previous state and then go to _/etc/php/7.2/apache2/ini.php_ (as `phpinfo()` site said) and uncomment `extension=pdo_odbc` and restart the apache2 like above command. After checked once again to `phpinfo()`, PDO Driver for mysql is gone instead (I have uncommented this one too). The information like screenshot that i attached above is not exist anymore. – Dont Bullying Me May 11 '20 at 18:49
  • is the information given by `# php -i | grep "Loaded Configuration File"` is reliable than `phpinfo()` ? or maybe i have been wrong somewhere. By the way i have tried to run `sudo curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo` still face the same rejected. – Dont Bullying Me May 11 '20 at 18:52
  • It's like having two different PHP versions, one is for console use only and one is for apache only. When you execute `php -i`, this is the cli/console version, not the one for Apache. – Daniel W. May 11 '20 at 19:28
  • Hmmm i thought all of php.ini under _/etc/php.._ are same. By the way according to your statement that _"Driver and extension are two different things..."_ because i just uncomment `extension=pdo_odbc` in my _php.ini_ and thoguht it would be enabled. I am little bit confusing, because in _Windows_ i just simply uncomment `extension=php_pdo_odbc.dll` and restart the apache and then the `pdo_odbc` already enabled. What do you think about that ? – Dont Bullying Me May 13 '20 at 09:50

1 Answers1

0

Driver and extension are not the same thing.

You can find the driver on microsoft.com website, like this or similiar for your specific version requirement:

https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

You could also try to install a GNU package like "FreeTDS".

The other problem is, you are editing the wrong php.ini file.

When running php -i | grep.... you are executing the cli SAPI of PHP in /etc/php/7.2/cli/php.ini, but what you actually want is the apache SAPI php.ini, most probably located at /etc/php/7.2/apache/php.ini. Go to your php_ini() URL, the correct path is shown in the first or second table presented.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • I will check for _php.ini_ regarding _PDO mysql_ is not exist on the list anymore. Regarding to installing Microsoft ODBC Driver, Should all the commands run one by one, or run at once ? (By the way i have taken wrong curl version, my bad). – Dont Bullying Me May 11 '20 at 19:14
  • @DontBullyingMe Sorry I cant get a working solution as I', neither using CentOS nor ODBC. I think there are already topics on Stackoverflow about instaling FreeTDS, unixODBC or Microsoft's ODBC. – Daniel W. May 11 '20 at 19:30
  • So i've installed _unixODBC v.2.3.4_ on _Ubuntu v.18.04.1_ using this command `# apt-get install unixodbc` after that i restart apache2 using this command `# /etc/init.d/apache2 restart`. But still pdo_odbc still unable in `phpinfo()` site, instead when i run my _connection.php_, it result result this error `Fatal error: Uncaught Error: Class 'PDO' not found in connection.php`. The previous was driver, now is PDO, i have no idea which part that i made wrong. – Dont Bullying Me May 13 '20 at 09:32
  • 1
    @DontBullyingMe Now you switched the operating system and PHP version. You need to install PDO. `apt-cache search pdo` – Daniel W. May 13 '20 at 12:30
  • There is no pdo list in ouput of your given command. These are a few of the output `php7.2-common - documentation, examples and common module for PHP php7.2-mysql - MySQL module for PHP php7.2-odbc - ODBC module for PHP php7.2-pgsql - PostgreSQL module for PHP php7.2-sqlite3 - SQLite3 module for PHP...`. – Dont Bullying Me May 14 '20 at 08:42
  • By the way i've solved the problem, the reason my setup is not set because all of my defined drivers / extensions path in _php.ini_ are on wrong, i am getting dizzy on linux. So `extension=extension=pdo`, `extension=extension=pdo_odbc`, `extension=extension=pdo_mysql` etc are not connected maybe because all of my drivers are not installed on default path, so i should add the path of each extensions so that the driver is listed on _php.ini _ like this `extension=extension=/usr/lib/php/12345/pdo_odbc.so`. – Dont Bullying Me May 14 '20 at 10:22
  • In short to enable extension in _php.ini_ on Linux in my case _Ubuntu 18.04_, first of all install desire driver with the proper package (to know the proper package `apt-cache search php-*`, or apt-cache search pdo) and then uncomment extension in php.ini like my comment above and then restart apache (i use this comment `# /etc/init.d/apache2 restart`). That's it. – Dont Bullying Me May 14 '20 at 10:26
  • About UnixODBC i just simply install it from this command `apt-get install unixodbc`. Because i faced error from guide that you suggest @Daniel W. Hmmm. Next coming to how to implement that unixodbc on PHP :-). – Dont Bullying Me May 14 '20 at 10:32
  • By the way i don't know my `PDO_ODBC` is enabled because of installed _unixodbc_ or only installed _pdo_. – Dont Bullying Me May 14 '20 at 10:34
  • @DontBullyingMe you may need to install different or additional packets, CentOS and Ubuntu are not the same. Have you tried packets like `php-mysql` or `php-pdo` or `php7-mysql`? – Daniel W. May 14 '20 at 12:54
  • I have installed those. Now i want to connect the my data source to php using _PDO_. – Dont Bullying Me May 15 '20 at 04:36
  • that's what i mean @Daniel W. – Dont Bullying Me May 15 '20 at 18:13
  • Hi Danniel, i have searched furthermore about connectiong .mdb to php using pdo in Linux. Would you help me ? I think i am almost done to make the connection. But unfortunately i am facing new issue. [link](https://stackoverflow.com/questions/61813881/how-to-access-mdb-data-source-in-php-linux) – Dont Bullying Me May 25 '20 at 12:05