Created
April 22, 2014 07:48
Generic PDO Prepare Statement Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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