CC-4346: Prepared statements - part 2

- adding common function to prepare param to PDO statement and excute.
This commit is contained in:
James 2012-09-05 15:59:15 -04:00
parent 71d4462272
commit 4ac2d6ac4e
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<?php
class Application_Common_Database{
public static function prepareAndExcute($sql, $paramValueMap, $type='all'){
$con = Propel::getConnection();
$stmt = $con->prepare($sql);
foreach ($paramValueMap as $param => $v) {
$stmt->bindValue($param, $v);
}
$rows = array();
if ($stmt->execute()) {
if ($type == 'single') {
$rows = $stmt->fetch(PDO::FETCH_ASSOC);
} else if ($type == 'column'){
$rows = $stmt->fetchColumn();
} else {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
} else {
$msg = implode(',', $stmt->errorInfo());
throw new Exception("Error: $msg");
}
return $rows;
}
}