Skip to content

Instantly share code, notes, and snippets.

@umayr
Created April 22, 2014 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umayr/11169060 to your computer and use it in GitHub Desktop.
Save umayr/11169060 to your computer and use it in GitHub Desktop.
Generic PDO Prepare Statement Function
function insert($table, $data)
{
$q = 'insert into ' . $table . ' values(';
for ($i = 0; $i < count($data); $i++) {
$q .= ($i == 0) ? '?' : ',?';
}
$q .= ');';
$dsn = 'mysql:host=' . DBHOST . ';dbname=' . DBNAME;
try {
$pdo = new PDO($dsn, DBUSER, DBPASS);
$stmt = $pdo->prepare($q, $data);
for ($i = 1; $i <= count($data); $i++) {
$stmt->bindParam($i, $data[$i - 1]);
}
$stmt->execute()? return true: return false;
} catch (PDOException $e) {
return array('err' => true, 'msg' => $e->getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment