Site icon David Yin's Blog

Make the PHP support Microsoft Access MDB file on Ubuntu 18.04

The very first step is to prepare the Ubuntu server.

I installed the Ubuntu 18.04 on a VM with 1CPU, 1024MB memory and 16GB storage room.

After the installation. Start the Ubuntu server and ssh to it.

Run the following command to keep it all updated.

sudo apt update
sudo apt upgrade

There is about 254 packages need to be upgraded.

Now it is time to start the installation.

Install Nginx web server, Mariadb server, php 7.2 support.

sudo apt install nginx
sudo apt install mariadb-server
sudo mysql-secure-installation
sudo apt install php7.2 php7.2-fpm php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip php7.2-json php7.2-curl

First, regarding nginx.

davidyin@php-mdb:~$ sudo apt install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common
nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx
nginx-common nginx-core
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,461 kB of archives.
After this operation, 8,210 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Regarding MariaDB installation

davidyin@php-mdb:~$ sudo apt install mariadb-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
galera-3 libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl
libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl
libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
libhttp-date-perl libhttp-message-perl libio-html-perl libjemalloc1
liblwp-mediatypes-perl libmysqlclient20 libterm-readkey-perl
libtimedate-perl liburi-perl mariadb-client-10.1 mariadb-client-core-10.1
mariadb-common mariadb-server-10.1 mariadb-server-core-10.1 mysql-common
socat
Suggested packages:
libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl
libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx mariadb-test
tinyca
The following NEW packages will be installed:
galera-3 libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl
libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl
libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
libhttp-date-perl libhttp-message-perl libio-html-perl libjemalloc1
liblwp-mediatypes-perl libmysqlclient20 libterm-readkey-perl
libtimedate-perl liburi-perl mariadb-client-10.1 mariadb-client-core-10.1
mariadb-common mariadb-server mariadb-server-10.1 mariadb-server-core-10.1
mysql-common socat
0 upgraded, 29 newly installed, 0 to remove and 0 not upgraded.
Need to get 24.2 MB of archives.
After this operation, 184 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Add root user password to MariaDB

davidyin@php-mdb:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
davidyin@php-mdb:~$

Install php7.2-fpm

 

davidyin@php-mdb:~$ sudo apt install php7.2 php7.2-fpm php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip php7.2-json php7.2-curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libsodium23 libzip4 php-common php7.2-opcache php7.2-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libsodium23 libzip4 php-common php7.2 php7.2-cli php7.2-common php7.2-curl
  php7.2-fpm php7.2-gd php7.2-intl php7.2-json php7.2-mbstring php7.2-mysql
  php7.2-opcache php7.2-readline php7.2-xml php7.2-zip
0 upgraded, 17 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,015 kB of archives.
After this operation, 21.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]

After that, fix the potential problem of connection with phpmyadmin

Setup the Nginx site

cd /etc/nginx/conf.d/
sudo nano mysite.conf

Paste the these code into the file and save it.
Run the following command to test the Nginx configuration and restart the Nginx service.

sudo nginx -t
sudo service nginx restart

Add a phpinfo page to test

cd ~
mkdir mysite.com
cd mysite.com
nano phpinfo.php

Add the following php code.

phpinfo();

In the browser, enter the address http://mysite.com/phpinfo.php

Install mdbtools etc.

sudo apt install mdbtools unixodbc odbc-mdbtools unixodbc-bin php7.2-odbc
sudo service php7.2-fpm restart

Then I upload the a Microsoft Access MDB file, sample.mdb and a mdb.php file.
This database file has a table, pt_users, which has two fields of user’s first name and last name. I will retrieve them.
The content of mdb.php as below:

$db = './sample.mdb';

$dbName  =  $db  ; 
$driver  =  'MDBTools'; 

$dbh  =  new  PDO("odbc:Driver=$driver;DBQ=$dbName;");
	
// Table with column num, name test 
   $sql  =  "SELECT userfirstname, userlastname FROM pt_users" ;  // The rules are the same as above 
   $sth  =  $dbh ->  prepare($sql); 
   $sth ->  execute(); 
   // Bind to each column 
   $sth ->  bindColumn(1,$fname); 
   $sth ->  bindColumn(2,$lname);

   // Because of binding to the column, only the judgment result will be returned to $ flg 
   $values  =  [];  // Array to assign the value 
   while (  $flg  =  $sth ->  fetch(PDO::FETCH_BOUND)){ 
             $values[]  =  [$fname,$lname ]; 
	   	echo $fname,' :  ',$lname,'
';
   }	

I got blank page and check the Nginx error log file at /var/log/nginx/error.log

2019/11/15 06:55:53 [error] 28826#28826: *7 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught PDOException: SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open cursor lib '/etc/libodbccr.so' : file no$
Stack trace:
#0 /home/davidyin/mysite.com/mdb.php(18): PDO->__construct('odbc:Driver=MDB...')
#1 {main}
thrown in /home/davidyin/mysite.com/mdb.php on line 18" while reading response header from upstream, client: 192.168.1.3, server: mysite.com, request: "GET /mdb.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", $

It said missing libodbccr.so.
Run the following command.

cd /usr/lib/x86_64-linux-gnu
sudo ln libodbccr.so.2 libodbccr.so

Then Reload the http://mysite.com/mdb.php
Got the correct result. Done.

 

At last, the environment:

Exit mobile version