Get Started

Medoo 1.2

Where Syntax

Query

Fetch

Transaction

Advanced

PDO object

Debug

Information

version: 1.2.1

insert

Insert new records in table

insert($table, $data)
Return: [number] The number of rows affected.
$database->insert("account", [
	"user_name" => "foo",
	"email" => "foo@bar.com",
	"age" => 25
]);

Last Insert ID

If you want the row ID after the insertion, you need to call the lastInsertId() alone and get it.

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

$account_id = $database->id();

Array serialization

By default, the array data will be serialized by serialize() before insertion, but you can assign it as JSON to be serialized by json_encode().

$database->insert("account", [
	"user_name" => "foo",
	"email" => "foo@bar.com",
	"age" => 25,
	"lang" => ["en", "fr", "jp", "cn"] // => 'a:4:{i:0;s:2:"en";i:1;s:2:"fr";i:2;s:2:"jp";i:3;s:2:"cn";}'
]);

$database->insert("account", [
	"user_name" => "foo",
	"email" => "foo@bar.com",
	"age" => 25,
	"(JSON) lang" => ["en", "fr", "jp", "cn"] // => '["en","fr","jp","cn"]'
]);

Multi-insertion

You can also insert data multiply.

$database->insert("account", [
	[
		"user_name" => "foo",
		"email" => "foo@bar.com",
		"age" => 25,
		"city" => "New York",
		"(JSON) lang" => ["en", "fr", "jp", "cn"]
	],
	[
		"user_name" => "bar",
		"email" => "bar@foo.com",
		"age" => 14,
		"city" => "Hong Kong",
		"(JSON) lang" => ["en", "jp", "cn"]
	]
]);

Using SQL functions

In some of special case, you may need to use SQL functions to process data. Just assign # in front of the column name, then the value will not be quoted.

$database->insert("account", [
	"user_name" => "bar",
	"#uid" => "UUID()"
]);