1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function generate_select_query ($connection, $type, $fields, $table, array $where = array(), array $group_by = array(), array $limit = array()) {
$query = "SELECT " . implode(',', $fields) . " FROM {$table}";
if (!emtpy($where)) {
$pieces = array();
foreach ($where as $key => $value) {
$value = mysql_real_escape_string($value, $connection);
$pieces[] = "$key=$value";
}
$query .= " WHERE " . implode(' AND ', $pieces);
}
if (!empty($group_by))
$query .= " GROUP BY " . implode(',', $group_by);
if (count($limit) == 1)
$query .= " LIMIT {$limit[0]}";
if (count($limit) == 2)
$query .= " LIMIT {$limit[0]},{$limit[1]}";
return $query;
} |