Skip to content

Instantly share code, notes, and snippets.

@gsdevme
Created August 20, 2011 19:24
Show Gist options
  • Save gsdevme/1159529 to your computer and use it in GitHub Desktop.
Save gsdevme/1159529 to your computer and use it in GitHub Desktop.
Query Setup
<?php
try {
$return = ( bool ) !preg_match('/(^INSERT|^DELETE|^UPDATE)/i', $query);
$stmt = $this->_pdo->prepare($query);
if ($args->count() !== 0) {
for ($args; $args->valid(); $args->next()) {
$value = $args->current();
$data = $this->_dataType($value);
$stmt->bindValue($args->key(), $data->value, $data->type);
}
}
if ($stmt->execute()) {
if ($return === true) {
$data = $stmt->fetchAll(PDO::FETCH_CLASS);
if (!empty($data)) {
return ( array ) $data;
}
return null;
}
if (preg_match('/^INSERT/', $query)) {
return ( int ) $this->lastInsertId();
}
return ( int ) $stmt->rowCount();
}
throw new ModuleException('Query failed, Query: ' . $stmt->queryString, 500, $e);
} catch (PDOException $e) {
switch($e->getCode()){
// This is a Integrity constraint violation, so we return false
case '23000':
return ( bool ) false;
// No data, could be from a SP
case '02000':
return null;
}
throw new ModuleException('Looks like your query syntax is wrong, Query: ' . $query, 500, $e);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment