Get Started

Where Syntax

Query

Transaction

Advanced

PDO object

Debug

Information

version: 1.1.3

insert

Insert new records in table

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

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().

$last_user_id = $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";}'
]);

$last_user_id = $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.

$last_user_id = $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.

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