Insert data into mysql table based on PDO

Inserts data from associative array to mysql table based on PDO.

php
fork add to bundle source
function mysql_insert_table($table, $data) { $handle = new PDO('mysql:host=127.0.0.1;dbname=db', 'user', 'pwd'); foreach ( $data as $key => $value ) { $values[] = "`{$key}` = :{$key}"; $bind[':' . $key] = $value; } $sql = 'INSERT INTO ' . $table . ' SET ' . implode(', ', $values); $st = $handle->prepare($sql); $st->execute($bind); return $handle->lastInsertId(); }

usage

load_function('mysql_insert_table'); $id = mysql_insert_table('users', ['email' => 'anyone@function.codes']); // will insert a row to users table and return its autoincrement ID
← how to use this in app?
create log ...inout