insert 插入

优质
小牛编辑
129浏览
2023-12-01

insert

插入数据到表中

insert($table, $data)
  • table [string]

    表名.

  • data [array]

    插入到表里的数据.

Return: [number] 返回插入的id
$last_user_id = $database->insert("account", [    "user_name" => "foo",    "email" => "foo@bar.com",    "age" => 25]);

序列化数组

默认情况下,你要将数组插入会被序列化serialize(), 也可以使用 JSON 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"]']);

多条数据插入

可以同时插入多条数据.

$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"]    ]]);

使用 SQL 函数

在一些特殊的情况下,您可能需要使用SQL函数来处理数据。在字段前加入#号即可

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