Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Advanced

Raw object

PDO object

Debug

Information

version: 1.5.6

Get Started

Using Medoo is extremely easy!

Requirement

PHP_PDO extension list

Name Driver
MySQL, MariaDB php_pdo_mysql
MSSQL (Windows) php_pdo_sqlsrv
MSSQL (Linux/UNIX) php_pdo_dblib / php_pdo_sqlsrv
Oracle php_pdo_oci
Oracle version 8 php_pdo_oci8
SQLite php_pdo_sqlite
PostgreSQL php_pdo_pgsql
Sybase php_pdo_dblib

PHP PDO driver installation

Medoo is requiring PHP with PDO supported. If you didn't install before, follow this step.

// Open php.ini and remove the ';' for the database extension you want to install.

// .dll is for Windows and .so for Linux/UNIX

// From
;extension=php_pdo_mysql.dll
;extension=php_pdo_mysql.so

// To
extension=php_pdo_mysql.dll
extension=php_pdo_mysql.so

// Save it, and restart PHP or Apache server.

// If PDO is installed successfully, you can find it on phpinfo().

Or install via terminal. The PDO_XXX extension will be enabled and configured automatically.

$ sudo apt-get install php5-mysql

Composer Installation

If you know about composer, it is easy to install and manage the version dependence. Just use this command to add Medoo to composer.json. Or you can edit it depend on your requirement.

$ composer require catfan/Medoo

And update the composer

$ composer update

Download Installation

The most simple way. Just download medoo.php and put it into the right directory and then require, and now you are ready!

require  'Medoo.php';

Configuration

Pass array of configuration for initialization and start a database connection.

// If you installed via composer, just use this code to requrie autoloader on the top of your projects.
require 'vendor/autoload.php';

// Using Medoo namespace
use Medoo\Medoo;

$database = new Medoo([
	// required
	'database_type' => 'mysql',
	'database_name' => 'name',
	'server' => 'localhost',
	'username' => 'your_username',
	'password' => 'your_password',

	// [optional]
	'charset' => 'utf8',
	'port' => 3306,

	// [optional] Table prefix
	'prefix' => 'PREFIX_',

	// [optional] Enable logging (Logging is disabled by default for better performance)
	'logging' => true,

	// [optional] MySQL socket (shouldn't be used with server and port)
	'socket' => '/tmp/mysql.sock',

	// [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
	'option' => [
		PDO::ATTR_CASE => PDO::CASE_NATURAL
	],

	// [optional] Medoo will execute those commands after connected to the database for initialization
	'command' => [
		'SET SQL_MODE=ANSI_QUOTES'
	]
]);

$database->insert("account", [
	"user_name" => "foo",
	"email" => "foo@bar.com"
]);

Customized DSN Connection

You can also use customized DSN to connect the database that Medoo didn't supported by default, especially for some new database requiring special DSN parameter for database connection, or if you want to add more DSN parameters value for connection than original one.

The format of DSN connection string:
{driver}:{key}={value};{key}={value}
$database = new Medoo([
	// Started using customized DSN connection
	'dsn' => [
		// The PDO driver name for DSN driver parameter
		'driver' => 'mydb',
		// The parameters with key and value for DSN
		'server' => '12.23.34.45',
		'port' => '8886'
	],
	// [optional] Medoo will have different handle method according to different database type
	'database_type' => 'mysql',

	'username' => 'your_username',
	'password' => 'your_password'
]);

// The final DSN connection string will be generated like this
mydb:server=12.23.34.45;port=8886

For MSSQL

Since Microsoft latest version of MSSQL database, it is using pdo_sqlsrv PHP extension for both Windows and Linux/UNIX platform. Medoo is using pdo_sqlsrv driver to connect MSSQL by default. You can check out their GitHub repo for more detail https://github.com/Microsoft/msphpsql. If you want to use pdo_dblib driver to connect old version database, you need to provide driver option for initialization.
$database = new Medoo([
	'database_type' => 'mysql',
	'database_name' => 'name',
	'server' => 'localhost',
	'username' => 'your_username',
	'password' => 'your_password',

	// [optional] If you want to force Medoo to use dblib driver for connecting MSSQL database
	'driver' => 'dblib'
]);

For SQLite

$database = new Medoo([
	'database_type' => 'sqlite',
	'database_file' => 'my/database/path/database.db'
]);

// Memory database
$database = new Medoo([
	'database_type' => 'sqlite',
	'database_file' => ':memory:'
]);