Skip to content

Instantly share code, notes, and snippets.

@rococodogs
Last active August 29, 2015 13:57
Show Gist options
  • Save rococodogs/9492510 to your computer and use it in GitHub Desktop.
Save rococodogs/9492510 to your computer and use it in GitHub Desktop.
converts enum fields from mysql into an array
<?php
function enumExplode($tableCol) {
$expl = explode(".", $tableCol);
$table = $expl[0];
$column = $expl[1];
$query = "show columns from {$table} where field = :column";
$res = querydb($query, array("column" => $column), $error);
$enum = $res[0]['Type'];
$enum = explode(",", str_replace(array("enum(", ")", "'"), "", $enum));
return $error ?: $enum;
}
/**
* queryDB
* -------
* @param string sql query
* @param array values for query
* @param array errors returned
*
*/
function queryDB($query, $values = array(), &$errors = null) {
$pdo = new PDO(DBINFO, DBUSER, DBPASS);
$stmt = $pdo->prepare($query);
$stmt->execute($values);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$errorInfo = $stmt->errorInfo();
$errors = empty($errorInfo[2]) ? false : $stmt->errorInfo();
$stmt->closeCursor();
return $res;
}
?>
<?php
querydb("select * where id = :id and name = :name", array("id" => $id, "name" => $name), $error);
$query = "select * where id = :id and name = :name";
$values = array(
"id" => $id,
"name" => $name
);
$results = querydb($query, $values, $err);
/*
$results = array(
0 => array(
"name" => name,
"id" => id
),
1 => array(
"name" => name,
"id" => id
)
);
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment