Skip to content

Instantly share code, notes, and snippets.

@ameliaikeda
Last active December 12, 2015 02:58
Show Gist options
  • Save ameliaikeda/4703298 to your computer and use it in GitHub Desktop.
Save ameliaikeda/4703298 to your computer and use it in GitHub Desktop.
function get_user_array() {
if (isset($_SESSION["id"])) {
$sid = $_SESSION["id"];
if ($query = $db->prepare("SELECT id, name, status from `users` where id=?")) {
$query->bind_param("i", $sid);
$query->execute();
$query->bind_result($id, $name, $status);
if (!$query->fetch()) {
$query->close();
$db->close();
return false; // clunky, but that's what abstraction layers are for.
}
$query->close();
$db->close();
return array("name" => $name, "status" => $status, "id" => $id);
} else {
return false;
}
} else {
return false;
}
// counting users:
if ($query = $db->prepare("SELECT a, b, c FROM table WHERE x = ?")) {
$res = array();
$query->bind_param("i", 3);
$query->bind_result($x, $y, $z);
$query->execute();
$rows = $query->num_rows;
while ($query->fetch()) {
array_push($res, array($x, $y, $z));
}
// closing/cleanup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment