Introduction
PHP is a very common technology used to create dynamic websites and the PDO extension makes it easy to access the database from PHP.
PDO stands for PHP Data Objects and represents a connection between PHP and a database server. It provides a data-access abstraction layer which means that, regardless of which database you are using, you use the same functions to issue queries and fetch data. For details and further information on PDO, please see the PDO part of the PHP Manual.
PDO is great for abstracting the database access in your PHP application. As long as the PDO database driver for the specific database is installed and standard SQL is used, PDO lets you switch database by simply changing the connection string.
PDO Mimer is a driver for the PDO extension and it allows the usage of PDO to interact with a Mimer SQL database.
See the example below to get a more hands-on look.
Requirements and prerequisites
The following requirements must be fulfilled to use PDO Mimer:
- PDO Mimer is supported on 64-bit Linux/Unix and Windows systems
- Mimer SQL version 11 (available at our Download page)
- PHP 8.1 or later
PDO Mimer is under MIT license and is open source.
The source code for PDO Mimer can be found on GitHub.
If you are experiencing any difficulties with PDO Mimer, its installation, usage or documentation, please create a new issue on GitHub. All feedback is appreciated to help us at Mimer Information Technology to improve our product.
Installation
Install PDO Mimer to your PHP installation’s extension folder (ext) by using one of the following methods:
Linux/unix, using pickle
# php pickle.phar install --source https://github.com/mimersql/pdo_mimer
Linux/unix, building from source
This method requires the php-dev package which can be installed by a package manager, for example using apt on Ubuntu:
# apt install php-dev
Then the installation can be made as follows:
# git clone https://github.com/mimersql/pdo_mimer && cd pdo_mimer # phpize # ./configure --with-pdo-mimer # make # make install
Windows
From https://github.com/mimersql/pdo_mimer/releases/latest, download one of the releases available, i.e. the one that corresponds to the PHP version you have installed. After downloading, extract the zip file and put the pdo_mimer.dll file in the PHP extension folder.
Enable PDO Mimer
Enable by either adding the Mimer SQL PDO extension to your php.ini configuration file, or by giving it as an option when running php:
# php -dextension=pdo_mimer
Note – If the Mimer SQL extension is not located in the PHP extension directory you can give the full path as argument instead.
Verify the installation
# php -m | grep pdo_mimer # pdo_mimer
⚠️ On Windows:
use php -m and read the output to see if pdo_mimer is in the list of enabled extensions
Example usage
The example shown below uses the PHP CLI and has a Mimer SQL database called “testdb” running.
Example source:
$employees = [ ['John Smith', 30], ['Jane Doe', 40], ]; $db = new PDO('mimer:dbname=testdb', 'MIMER_USER', 'MIMER_PWD'); $db->exec(<<<SQL CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT ) SQL); $stmt = $db->prepare('INSERT INTO employees (id, name, age) VALUES (?, ?, ?)'); foreach ( $employees as $id => [$name, $age]) { $stmt->bindValue(1, $id, PDO::PARAM_INT); $stmt->bindValue(2, $name, PDO::PARAM_STR); $stmt->bindValue(3, $age, PDO::PARAM_INT); if ($id != array_key_last($employees)) { $stmt->mimerAddBatch(); } } $stmt->execute(); $stmt = $db->query('select name, age from employees'); while ($row = $stmt->fetch(PDO::FETCH_NUM)) { print "Name: {$row[0]} "; print "Age: {$row[1]}\n"; } $stmt = NULL; $db->exec("drop table employees"); $db = NULL;
Start php interactively and paste the source code above and you should see something like this:
$ php -dextension=pdo_mimer -a php > $db = new PDO('mimer:dbname=testdb', 'MIMER_USER', 'MIMER_PWD'); php > $db->exec(<<<SQL <<< > CREATE TABLE employees ( <<< > id INT PRIMARY KEY, <<< > name VARCHAR(50) NOT NULL, <<< > age INT <<< > ) <<< > SQL); php > php > $employees = [ php > ['John Smith', 30], php > ['Jane Doe', 40], php > ]; php > php > $stmt = $db->prepare('INSERT INTO employees (id, name, age) VALUES (?, ?, ?)'); php > foreach ( $employees as $id => [$name, $age]) { php { $stmt->bindValue(1, $id, PDO::PARAM_INT); php { $stmt->bindValue(2, $name, PDO::PARAM_STR); php { $stmt->bindValue(3, $age, PDO::PARAM_INT); php { if ($id != array_key_last($employees)) php { $stmt->mimerAddBatch(); php { } php > $stmt->execute(); php > php > $stmt = $db->query('SELECT id, name, age FROM employees'); php > while ($row = $stmt->fetch(PDO::FETCH_NUM)) { php { print "Name: {$row[0]} "; php { print "Age: {$row[1]}\n"; php { } Name: John Smith Age: 30 Name: Jane Doe Age: 40 php > $stmt = NULL; php > $db->exec("drop table employees"); php > $db = NULL;
Important Links
- PDO Mimer on Github: https://github.com/mimersql/pdo_mimer
- PDO Mimer issue handling: https://github.com/mimersql/pdo_mimer/issues
- PHP home page: https://php.net
- Location for all Mimer SQL documentation: https://docs.mimer.com
- Pickle (the PHP extension manager): https://github.com/FriendsOfPHP/pickle