Get Started


Where Syntax







Raw object

PDO object



version: 1.7.10

Get Started

Using Medoo is extremely easy!


PHP_PDO extension list

The following list indicated which PDO extension should be installed according the type and version of database or the system platform.

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 requires PHP with PDO supported. If you didn't install it before, follow this step.

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

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

// From

// To

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

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

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

$ sudo apt-get install php5-mysql

For PHP 7.0 or above

$ sudo apt-get install php7.0-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 the file, and now you are ready!

require  'Medoo.php';


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

// If you installed via composer, just use this code to require 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' => 'utf8mb4',
	'collation' => 'utf8mb4_general_ci',
	'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
	'option' => [

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

$database->insert("account", [
	"user_name" => "foo",
	"email" => ""

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:
$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' => '',
		'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

PDO object initialization

You can also create Medoo by passing initialized and connected PDO object.

$pdo = new PDO('mysql:dbname=test;host=', 'user', 'password');

$database = new Medoo([
	// Initialized and connected PDO object
	'pdo' => $pdo,

	// [optional] Medoo will have different handle method according to different database type
	'database_type' => 'mysql'


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 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' => 'mssql',
	'database_name' => 'name',
	'server' => 'localhost',
	'username' => 'your_username',
	'password' => 'your_password',

	// [optional] The application name
	'appname' => 'test',

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

Medoo is also supported those options for connecting MSSQL database while using sqlsrv driver. You can check out more detail from

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

	// [optional] MSSQL connection options
	'application_intent' => 'ReadOnly',
	'attach_db_file_name' => './database.sql',
	'authentication' => 'SqlPassword',
	'column_encryption' => 'Enabled',
	'connection_pooling' => 1,
	'encrypt' => 1,
	'failover_partner' => 'MultiSubnetFailover',
	'key_store_authentication' => 'KeyVaultPassword',
	'key_store_principal_id' => 'AzureName',
	'key_store_secret' => 'AzurePass',
	'login_timeout' => '20',
	'multiple_active_result_sets' => 1,
	'multi_subnet_failover' => 'Yes',
	'scrollable' => 'buffered',
	'trace_file' => './path',
	'trace_on' => 1,
	'transaction_isolation' => PDO::SQLSRV_TXN_SNAPSHOT,
	'transparent_network_ip_resolution' => 'Enabled',
	'trust_server_certificate' => 1,
	'wsid' => 'Computer1'

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:'

Debug DSN String

You can check out the DSN string from info() API.

$database = new Medoo([
	'database_type' => 'mysql',
	'database_name' => 'test',
	'server' => '',
	'username' => 'your_username',
	'password' => 'your_password',

echo $database->info()['dsn'];

// mysql:dbname=test;host=