version: 2.2.1
Get Started
Getting started with Medoo is straightforward.
Requirements
- PHP 7.3+ with PDO extension installed.
- A SQL database such as MySQL, MSSQL, SQLite, or another supported engine.
- Make sure the corresponding PDO driver is installed and enabled.
- Basic SQL knowledge.
PHP PDO Extension List
The following list shows which PDO extension to install based on your database type and platform.
| Name | Driver |
| MySQL, MariaDB | php_pdo_mysql |
| MSSQL | php_pdo_sqlsrv / php_pdo_dblib |
| Oracle | php_pdo_oci |
| SQLite | php_pdo_sqlite |
| PostgreSQL | php_pdo_pgsql |
| Sybase | php_pdo_dblib |
PHP PDO Driver Installation
Medoo requires PHP with PDO support. If PDO is not installed yet, follow these steps.
// Edit php.ini and remove the leading semicolon for the database extension you want to enable. // .dll is used on Windows. .so is used on Linux/UNIX. // Before ;extension=php_pdo_mysql.dll ;extension=php_pdo_mysql.so // After extension=php_pdo_mysql.dll extension=php_pdo_mysql.so // Save the file, then restart PHP or Apache/Nginx. // If PDO is installed correctly, it appears in phpinfo().
Or install from the terminal. The PDO extension will be enabled and configured automatically.
$ sudo apt-get install php7.4-mysql
Composer Installation
If you use Composer, install Medoo with the following command.
$ composer require catfan/medoo
Composer installs the dependency automatically. Run
composer update only if you need to refresh dependencies manually.
$ composer update
Download Installation
A simple alternative is to download
medoo.php, place it in your project, and require it.require 'Medoo.php';
Configuration
Pass a configuration array to initialize Medoo and start a database connection.
// Load Composer's autoloader. require 'vendor/autoload.php'; // Import the Medoo namespace. use Medoo\Medoo; $database = new Medoo([ // Required. 'type' => 'mysql', 'host' => 'localhost', 'database' => 'name', 'username' => 'your_username', 'password' => 'your_password', // Optional. 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'port' => 3306, // Optional: table prefix. All table names become PREFIX_table. 'prefix' => 'PREFIX_', // Optional: enable logging. Disabled by default for better performance. 'logging' => true, // Optional. // Error mode. // Defines how PDO reports errors. // PDO::ERRMODE_SILENT (default) | PDO::ERRMODE_WARNING | PDO::ERRMODE_EXCEPTION // Reference: https://www.php.net/manual/en/pdo.error-handling.php. 'error' => PDO::ERRMODE_SILENT, // Optional. // Driver options for the connection. // Reference: http://www.php.net/manual/en/pdo.setattribute.php. 'option' => [ PDO::ATTR_CASE => PDO::CASE_NATURAL ], // Optional: commands to run after the database connection is established. 'command' => [ 'SET SQL_MODE=ANSI_QUOTES' ] ]);
Custom DSN Connection
You can also provide a custom DSN to connect databases that Medoo does not support out of the box, or to pass additional DSN parameters.
DSN Connection String Format
{driver}:{key}={value};{key}={value}
$database = new Medoo([ 'dsn' => [ // PDO driver name used in the DSN. 'driver' => 'mydb', // DSN parameters as key-value pairs. 'server' => '12.23.34.45', 'port' => '8886' ], // Optional: tells Medoo which database-specific behavior to use. 'type' => 'mysql', 'username' => 'your_username', 'password' => 'your_password' ]); // Final generated DSN string: mydb:server=12.23.34.45;port=8886
PDO Object Initialization
You can also create Medoo from an existing initialized PDO object.
$pdo = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'password');
$database = new Medoo([
// Reuse an initialized and connected PDO instance.
'pdo' => $pdo,
// Optional: tells Medoo which database-specific behavior to use.
'type' => 'mysql'
]);
For MySQL
This option is for MySQL only.
$database = new Medoo([ // Optional: MySQL socket (do not use with server and port together). 'socket' => '/tmp/mysql.sock', ]);
For MariaDB
MariaDB is handled the same as MySQL. The database type is mapped to
mysql automatically.$database = new Medoo([ 'type' => 'mariadb', 'host' => 'localhost', 'database' => 'name', 'username' => 'your_username', 'password' => 'your_password', ]);
For MSSQL
Modern MSSQL connections typically use the pdo_sqlsrv extension on both Windows and Linux/UNIX. Medoo uses the pdo_sqlsrv driver by default. For legacy environments that require pdo_dblib, set the driver explicitly during initialization. See https://github.com/Microsoft/msphpsql for details.
$database = new Medoo([ 'type' => 'mssql', 'host' => 'localhost', 'database' => 'name', 'username' => 'your_username', 'password' => 'your_password', // Optional: application name. 'appname' => 'test', // Optional. // Force Medoo to use the dblib driver for MSSQL. // Default: sqlsrv. 'driver' => 'dblib' ]);
Medoo also supports MSSQL connection options when using the sqlsrv driver. See details at https://docs.microsoft.com/en-us/sql/connect/php/connection-options?view=sql-server-2017.
- ApplicationIntent
- AttachDBFileName
- Authentication
- ColumnEncryption
- ConnectionPooling
- Encrypt
- Failover_Partner
- KeyStoreAuthentication
- KeyStorePrincipalId
- KeyStoreSecret
- LoginTimeout
- MultipleActiveResultSets
- MultiSubnetFailover
- Scrollable
- TraceFile
- TraceOn
- TransactionIsolation
- TransparentNetworkIPResolution
- TrustServerCertificate
- WSID
$database = new Medoo([ 'type' => 'mssql', 'host' => 'localhost', 'database' => 'name', '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
File Database
$database = new Medoo([ 'type' => 'sqlite', 'database' => 'my/database/path/database.db' ]);
Memory Database
$database = new Medoo([ 'type' => 'sqlite', 'database' => ':memory:' ]);
Temporary Database
The temporary database is deleted when the connection is closed.
$database = new Medoo([ 'type' => 'sqlite', 'database' => '' ]); // Or omit the database option entirely. $database = new Medoo([ 'type' => 'sqlite' ]);
Debug DSN String
You can inspect the final DSN string by calling
info().$database = new Medoo([ 'type' => 'mysql', 'host' => '127.0.0.1', 'database' => 'test', 'username' => 'your_username', 'password' => 'your_password', ]); echo $database->info()['dsn']; // mysql:dbname=test;host=127.0.0.1
Test Mode
This mode is useful for tests. You can verify generated SQL through
$database->queryString across different database types. Queries are generated but not executed.$database = new Medoo([
'testMode' => true
]);
// Set the database type.
$database->type = 'mysql';
$database->select("account", ["user_name"], ["user_id[>]" => 10]);
echo $database->queryString;