Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Created December 25, 2015 14:32
Show Gist options
  • Save noodlehaus/e1149fb8a29b4e8f0daa to your computer and use it in GitHub Desktop.
Save noodlehaus/e1149fb8a29b4e8f0daa to your computer and use it in GitHub Desktop.
mysqli + extras, php7
<?php declare(strict_types=1);
# Executes a select and returns a single row.
function mysqli_select_one($db, string $sql, ...$params) {
$stmt = mysqli_interpolate($db, $sql, ...$params);
if (
!mysqli_stmt_execute($stmt) ||
false === ($result = mysqli_stmt_get_result($stmt))
) {
throw new mysqli_sql_exception(
mysqli_stmt_error($stmt),
mysqli_stmt_errno($stmt)
);
}
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
mysqli_stmt_close($stmt);
return $row;
}
# Executes a select and returns all resulting rows
function mysqli_select_all($db, string $sql, ...$params) : array {
$stmt = mysqli_interpolate($db, $sql, ...$params);
if (
!mysqli_stmt_execute($stmt) ||
(false === ($result = mysqli_stmt_get_result($stmt)))
) {
throw new mysqli_sql_exception(
mysqli_stmt_error($stmt),
mysqli_stmt_errno($stmt)
);
}
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
mysqli_stmt_close($stmt);
return $rows;
}
# Executes an update query and returns affected row count.
function mysqli_update($db, string $sql, ...$params) : int {
$stmt = mysqli_interpolate($db, $sql, ...$params);
if (!mysqli_stmt_execute($stmt)) {
throw new mysqli_sql_exception(
mysqli_stmt_error($stmt),
mysqli_stmt_errno($stmt)
);
}
$affected = mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
return $affected;
}
# Executes an insert and returns the insert id if any.
function mysqli_insert($db, string $sql, ...$params) : string {
$stmt = mysqli_interpolate($db, $sql, ...$params);
if (!mysqli_stmt_execute($stmt)) {
throw new mysqli_sql_exception(
mysqli_stmt_error($stmt),
mysqli_stmt_errno($stmt)
);
}
$id = mysqli_insert_id($db);
mysqli_stmt_close($stmt);
return (string) $id;
}
# how mysqli_prepare should be
function mysqli_interpolate($db, string $sql, ...$args) : mysqli_stmt {
$argn = count($args);
$stmt = mysqli_prepare($db, $sql);
if ($stmt === false) {
throw new mysqli_sql_exception(mysqli_error($db), mysqli_errno($db));
}
if ($argn) {
$syms = implode('', array_pad([], $argn, 's'));
$refs = [];
foreach ($args as $key => $val) {
$refs[$key] = &$args[$key];
}
array_unshift($refs, $stmt, $syms);
if (false === call_user_func_array('mysqli_stmt_bind_param', $refs)) {
throw new mysqli_sql_exception(
mysqli_stmt_error($stmt),
mysqli_stmt_errno($stmt)
);
}
}
return $stmt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment