Get Started

Medoo 1.2

Where Syntax

Query

Fetch

Transaction

Advanced

PDO object

Debug

Information

version: 1.2.1

Get Started

Using Medoo is extremely easy!

Requirement

Tips

On PHP 5.4+, you can use [] as short array syntax. All Medoo example code are using [] instead of array().

// On PHP 5.1
$data = array("foo", "bar");

// On PHP 5.4+
$data = ["foo", "bar"];

php_pdo extension list

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.

// From
;extension=php_pdo_mysql.dll

// To
extension=php_pdo_mysql.dll

// 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

There are three ways to configure medoo 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',
	'charset' => 'utf8',

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

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

	// [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

If you want to use MSSQL with Medoo under PHP platform, you need to install pdo_sqlsrv PHP extension for Windows or pdo_dblib for Linux/UNIX. pdo_mssql extension is deprecated and will be removed from PHP.

For SQLite

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

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