Added more typesafe shorthands for fetching database operations.

This commit is contained in:
flash 2019-01-09 20:06:02 +01:00
parent 44dc4ae2ce
commit 9505cb1f0b
26 changed files with 95 additions and 138 deletions

View file

@ -317,7 +317,7 @@ MIG;
WHERE `user_id` = :user_id
');
$getUserDisplayInfo->bindValue('user_id', $mszUserId);
$userDisplayInfo = $getUserDisplayInfo->execute() ? $getUserDisplayInfo->fetch(\PDO::FETCH_ASSOC) : [];
$userDisplayInfo = db_fetch($getUserDisplayInfo);
if ($userDisplayInfo) {
$userDisplayInfo['general_perms'] = perms_get_user(MSZ_PERMS_GENERAL, $userDisplayInfo['user_id']);

View file

@ -82,7 +82,7 @@ switch ($authMode) {
WHERE `user_id` = :user_id
');
$getResetUser->bindValue('user_id', $resetUser);
$resetUser = $getResetUser->execute() ? $getResetUser->fetch(PDO::FETCH_ASSOC) : [];
$resetUser = db_fetch($getResetUser);
if (empty($resetUser)) {
header('Location: /auth.php?m=forgot');
@ -156,7 +156,7 @@ switch ($authMode) {
WHERE LOWER(`email`) = LOWER(:email)
');
$forgotUser->bindValue('email', $authEmail);
$forgotUser = $forgotUser->execute() ? $forgotUser->fetch(PDO::FETCH_ASSOC) : [];
$forgotUser = db_fetch($forgotUser);
if (empty($forgotUser)) {
tpl_var('auth_forgot_error', 'This user is not registered with us.');
@ -239,7 +239,7 @@ MSG;
');
$getUser->bindValue('email', $authUsername);
$getUser->bindValue('username', $authUsername);
$userData = $getUser->execute() ? $getUser->fetch() : [];
$userData = db_fetch($getUser);
$userId = (int)($userData['user_id'] ?? 0);
$loginFailedError = sprintf(

View file

@ -27,7 +27,7 @@ if ($changelogChange > 0) {
WHERE `change_id` = :change_id
');
$getChange->bindValue('change_id', $changelogChange);
$change = $getChange->execute() ? $getChange->fetch(PDO::FETCH_ASSOC) : [];
$change = db_fetch($getChange);
if (!$change) {
echo render_error(404);
@ -42,8 +42,7 @@ if ($changelogChange > 0) {
WHERE ct.`change_id` = :change_id
');
$getTags->bindValue('change_id', $change['change_id']);
$tags = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : [];
tpl_var('tags', $tags);
tpl_var('tags', db_fetch_all($getTags));
}
echo tpl_render('changelog.change', [

View file

@ -61,7 +61,7 @@ if (!empty($forumId)) {
WHERE `forum_id` = :forum_id
');
$getForum->bindValue('forum_id', $forumId);
$forum = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false;
$forum = db_fetch($getForum);
}
if (empty($forum)) {
@ -236,7 +236,7 @@ $getDisplayInfo = db_prepare('
WHERE `user_id` = :user_id
');
$getDisplayInfo->bindValue('user_id', user_session_current('user_id'));
$displayInfo = $getDisplayInfo->execute() ? $getDisplayInfo->fetch(PDO::FETCH_ASSOC) : [];
$displayInfo = db_fetch($getDisplayInfo);
echo tpl_render('forum.posting', [
'posting_breadcrumbs' => forum_get_breadcrumbs($forumId),

View file

@ -44,7 +44,7 @@ switch ($_GET['v'] ?? null) {
');
$getChanges->bindValue('take', $changelogPagination['range']);
$getChanges->bindValue('offset', $changelogOffset);
$changes = $getChanges->execute() ? $getChanges->fetchAll(PDO::FETCH_ASSOC) : [];
$changes = db_fetch_all($getChanges);
$getTags = db_prepare('
SELECT
@ -58,7 +58,7 @@ switch ($_GET['v'] ?? null) {
// grab tags
for ($i = 0; $i < count($changes); $i++) {
$getTags->bindValue('change_id', $changes[$i]['change_id']);
$changes[$i]['tags'] = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : [];
$changes[$i]['tags'] = db_fetch_all($getTags);
}
echo tpl_render('manage.changelog.changes', [
@ -170,7 +170,7 @@ switch ($_GET['v'] ?? null) {
WHERE `change_id` = :change_id
');
$getChange->bindValue('change_id', $changeId);
$change = $getChange->execute() ? $getChange->fetch(PDO::FETCH_ASSOC) : [];
$change = db_fetch($getChange);
if ($change) {
tpl_var('edit_change', $change);
@ -185,7 +185,7 @@ switch ($_GET['v'] ?? null) {
)
');
$assignedTags->bindValue('change_id', $change['change_id']);
$assignedTags = $assignedTags->execute() ? $assignedTags->fetchAll(PDO::FETCH_ASSOC) : [];
$assignedTags = db_fetch_all($assignedTags);
$availableTags = db_prepare('
SELECT `tag_id`, `tag_name`
@ -198,7 +198,7 @@ switch ($_GET['v'] ?? null) {
)
');
$availableTags->bindValue('change_id', $change['change_id']);
$availableTags = $availableTags->execute() ? $availableTags->fetchAll(PDO::FETCH_ASSOC) : [];
$availableTags = db_fetch_all($availableTags);
tpl_vars([
'edit_change_assigned_tags' => $assignedTags,
@ -234,7 +234,7 @@ switch ($_GET['v'] ?? null) {
FROM `msz_changelog_actions` as a
ORDER BY a.`action_id` ASC
');
tpl_var('changelog_actions', $getActions->execute() ? $getActions->fetchAll(PDO::FETCH_ASSOC) : []);
tpl_var('changelog_actions', db_fetch_all($getActions));
}
if ($canManageTags) {
@ -249,7 +249,7 @@ switch ($_GET['v'] ?? null) {
FROM `msz_changelog_tags` as t
ORDER BY t.`tag_id` ASC
');
tpl_var('changelog_tags', $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []);
tpl_var('changelog_tags', db_fetch_all($getTags));
}
echo tpl_render('manage.changelog.actions_tags');
@ -307,7 +307,7 @@ switch ($_GET['v'] ?? null) {
WHERE `tag_id` = :tag_id
');
$getTag->bindValue('tag_id', $tagId);
$tag = $getTag->execute() ? $getTag->fetch(PDO::FETCH_ASSOC) : [];
$tag = db_fetch($getTag);
if ($tag) {
tpl_var('edit_tag', $tag);
@ -381,7 +381,7 @@ switch ($_GET['v'] ?? null) {
WHERE `action_id` = :action_id
');
$getAction->bindValue('action_id', $actionId);
$action = $getAction->execute() ? $getAction->fetch(PDO::FETCH_ASSOC) : [];
$action = db_fetch($getAction);
if ($action) {
tpl_var('edit_action', $action);

View file

@ -23,7 +23,7 @@ switch ($_GET['v'] ?? null) {
WHERE `forum_id` = :forum_id
');
$getForum->bindValue('forum_id', (int)($_GET['f'] ?? 0));
$forum = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false;
$forum = db_fetch($getForum);
if (!$forum) {
echo render_error(404);

View file

@ -45,7 +45,7 @@ switch ($_GET['v'] ?? null) {
');
$getManageUsers->bindValue('offset', $usersOffset);
$getManageUsers->bindValue('take', $usersPagination['range']);
$manageUsers = $getManageUsers->execute() ? $getManageUsers->fetchAll() : [];
$manageUsers = db_fetch_all($getManageUsers);
tpl_vars([
'manage_users' => $manageUsers,
@ -80,9 +80,9 @@ switch ($_GET['v'] ?? null) {
');
$getUser->bindValue('user_id', $userId);
$getUser->execute();
$manageUser = $getUser->execute() ? $getUser->fetch() : [];
$manageUser = db_fetch($getUser);
if (!$manageUser) {
if (empty($manageUser)) {
echo 'Could not find that user.';
break;
}
@ -97,7 +97,7 @@ switch ($_GET['v'] ?? null) {
)
');
$getHasRoles->bindValue('user_id', $manageUser['user_id']);
$hasRoles = $getHasRoles->execute() ? $getHasRoles->fetchAll() : [];
$hasRoles = db_fetch_all($getHasRoles);
$getAvailableRoles = db_prepare('
SELECT `role_id`, `role_name`
@ -109,7 +109,7 @@ switch ($_GET['v'] ?? null) {
)
');
$getAvailableRoles->bindValue('user_id', $manageUser['user_id']);
$availableRoles = $getAvailableRoles->execute() ? $getAvailableRoles->fetchAll() : [];
$availableRoles = db_fetch_all($getAvailableRoles);
if ($canManagePerms) {
tpl_var('permissions', $permissions = manage_perms_list(perms_get_user_raw($userId)));
@ -283,7 +283,7 @@ switch ($_GET['v'] ?? null) {
');
$getManageRoles->bindValue('offset', $rolesOffset);
$getManageRoles->bindValue('take', $rolesPagination['range']);
$manageRoles = $getManageRoles->execute() ? $getManageRoles->fetchAll() : [];
$manageRoles = db_fetch_all($getManageRoles);
echo tpl_render('manage.users.roles', [
'manage_roles' => $manageRoles,
@ -458,9 +458,9 @@ switch ($_GET['v'] ?? null) {
WHERE `role_id` = :role_id
');
$getEditRole->bindValue('role_id', $roleId);
$editRole = $getEditRole->execute() ? $getEditRole->fetch() : [];
$editRole = db_fetch($getEditRole);
if (!$editRole) {
if (empty($editRole)) {
echo 'invalid role';
break;
}

View file

@ -70,9 +70,9 @@ $getRole = db_prepare('
WHERE `role_id` = :role_id
');
$getRole->bindValue('role_id', $roleId);
$role = $getRole->execute() ? $getRole->fetch(PDO::FETCH_ASSOC) : [];
$role = db_fetch($getRole);
if (!$role) {
if (empty($role)) {
echo render_error(404);
return;
}
@ -115,7 +115,7 @@ $getUsers = db_prepare(sprintf(
$getUsers->bindValue('role_id', $role['role_id']);
$getUsers->bindValue('offset', $usersOffset);
$getUsers->bindValue('take', $usersPagination['range']);
$users = $getUsers->execute() ? $getUsers->fetchAll(PDO::FETCH_ASSOC) : [];
$users = db_fetch_all($getUsers);
echo tpl_render('user.listing', [
'roles' => $roles,

View file

@ -101,7 +101,7 @@ switch ($mode) {
) as `user_id`
');
$getUserId->bindValue('user_id', $_GET['u'] ?? 0);
$userId = $getUserId->execute() ? ($getUserId->fetch(PDO::FETCH_ASSOC)['user_id'] ?? 0) : 0;
$userId = (int)($getUserId->execute() ? $getUserId->fetchColumn(1) : 0);
if ($userId < 1) {
http_response_code(404);
@ -347,7 +347,7 @@ switch ($mode) {
)
);
$getProfile->bindValue('user_id', $userId);
$profile = $getProfile->execute() ? $getProfile->fetch(PDO::FETCH_ASSOC) : [];
$profile = db_fetch($getProfile);
$backgroundPath = build_path(MSZ_STORAGE, 'backgrounds/original', "{$profile['user_id']}.msz");

View file

@ -182,7 +182,7 @@ $getUserRoles = db_prepare('
ORDER BY r.`role_hierarchy` DESC
');
$getUserRoles->bindValue('user_id', user_session_current('user_id'));
$userRoles = $getUserRoles->execute() ? $getUserRoles->fetchAll(PDO::FETCH_ASSOC) : [];
$userRoles = db_fetch_all($getUserRoles);
echo tpl_render('user.settings', [
'errors' => $errors,

View file

@ -98,9 +98,7 @@ function forum_fetch(int $forumId, bool $showDeleted = false): array
$showDeleted ? '' : 'AND `topic_deleted` IS NULL'
));
$getForum->bindValue('forum_id', $forumId);
$forums = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false;
return $forums ? $forums : [];
return db_fetch($getForum);
}
function forum_get_root_categories(int $userId): array
@ -129,8 +127,7 @@ function forum_get_root_categories(int $userId): array
));
$getCategories->bindValue('perm_user_id_user', $userId);
$getCategories->bindValue('perm_user_id_role', $userId);
$categories = $getCategories->execute() ? $getCategories->fetchAll(PDO::FETCH_ASSOC) : [];
$categories = array_merge([MSZ_FORUM_ROOT_DATA], $categories);
$categories = array_merge([MSZ_FORUM_ROOT_DATA], db_fetch_all($getCategories));
$getRootForumCount = db_prepare(sprintf(
"
@ -173,7 +170,7 @@ function forum_get_breadcrumbs(
SELECT * FROM breadcrumbs
');
$getBreadcrumbs->bindValue('forum_id', $forumId);
$breadcrumbsDb = $getBreadcrumbs->execute() ? $getBreadcrumbs->fetchAll(PDO::FETCH_ASSOC) : [];
$breadcrumbsDb = db_fetch_all($getBreadcrumbs);
if (!$breadcrumbsDb) {
return [$indexLink];
@ -208,7 +205,7 @@ function forum_get_colour(int $forumId): int
SELECT * FROM breadcrumbs
');
$getColours->bindValue('forum_id', $forumId);
$colours = $getColours->execute() ? $getColours->fetchAll(PDO::FETCH_ASSOC) : [];
$colours = db_fetch_all($getColours);
if ($colours) {
foreach ($colours as $colour) {
@ -380,7 +377,7 @@ function forum_get_children(int $parentId, int $userId, bool $showDeleted = fals
$getListing->bindValue('user_for_check', $userId);
$getListing->bindValue('parent_id', $parentId);
return $getListing->execute() ? $getListing->fetchAll(PDO::FETCH_ASSOC) : [];
return db_fetch_all($getListing);
}
function forum_timeout(int $forumId, int $userId): int

View file

@ -93,10 +93,8 @@ function forum_perms_get_role(string $prefix, int $forum, int $role): int
function forum_perms_get_user_raw(?int $forum, int $user): array
{
$emptyPerms = forum_perms_create();
if ($user < 1) {
return $emptyPerms;
return forum_perms_create();
}
$getPerms = db_prepare(sprintf('
@ -113,15 +111,10 @@ function forum_perms_get_user_raw(?int $forum, int $user): array
}
$getPerms->bindValue('user_id', $user);
$perms = db_fetch($getPerms);
if (!$getPerms->execute()) {
return $emptyPerms;
}
$perms = $getPerms->fetch(PDO::FETCH_ASSOC);
if (!$perms) {
return $emptyPerms;
if (empty($perms)) {
return forum_perms_create();
}
return $perms;
@ -129,10 +122,8 @@ function forum_perms_get_user_raw(?int $forum, int $user): array
function forum_perms_get_role_raw(?int $forum, ?int $role): array
{
$emptyPerms = forum_perms_create();
if ($role < 1 && $role !== null) {
return $emptyPerms;
return forum_perms_create();
}
$getPerms = db_prepare(sprintf(
@ -156,14 +147,10 @@ function forum_perms_get_role_raw(?int $forum, ?int $role): array
$getPerms->bindValue('role_id', $role);
}
if (!$getPerms->execute()) {
return $emptyPerms;
}
$perms = db_fetch($getPerms);
$perms = $getPerms->fetch(PDO::FETCH_ASSOC);
if (!$perms) {
return $emptyPerms;
if (empty($perms)) {
return forum_perms_create();
}
return $perms;

View file

@ -77,8 +77,7 @@ function forum_post_find(int $postId, int $userId): array
$getPostInfo->bindValue('post_id', $postId);
$getPostInfo->bindValue('perm_user_id_user', $userId);
$getPostInfo->bindValue('perm_user_id_role', $userId);
$postInfo = $getPostInfo->execute() ? $getPostInfo->fetch(PDO::FETCH_ASSOC) : false;
return $postInfo ? $postInfo : [];
return db_fetch($getPostInfo);
}
function forum_post_get(int $postId, bool $allowDeleted = false): array
@ -117,8 +116,7 @@ function forum_post_get(int $postId, bool $allowDeleted = false): array
$allowDeleted ? '' : 'AND `post_deleted` IS NULL'
));
$getPost->bindValue('post_id', $postId);
$post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
return $post ? $post : [];
return db_fetch($getPost);
}
function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false): array
@ -166,5 +164,5 @@ function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $
$getPosts->bindValue('take', $take);
}
return $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : [];
return db_fetch_all($getPosts);
}

View file

@ -100,8 +100,7 @@ function forum_topic_fetch(int $topicId, int $userId = 0): array
$getTopic->bindValue('topic_id', $topicId);
$getTopic->bindValue('perm_user_id_user', $userId);
$getTopic->bindValue('perm_user_id_role', $userId);
$topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false;
return $topic ? $topic : [];
return db_fetch($getTopic);
}
function forum_topic_bump(int $topicId): bool
@ -223,5 +222,5 @@ function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $ta
$getTopics->bindValue('take', $take);
}
return $getTopics->execute() ? $getTopics->fetchAll() : [];
return db_fetch_all($getTopics);
}

View file

@ -197,6 +197,5 @@ function ip_blacklist_list(): array
CONCAT(INET6_NTOA(`ip_subnet`), '/', `ip_mask`) as `ip_cidr`
FROM `msz_ip_blacklist`
");
$blacklist = $getBlacklist->execute() ? $getBlacklist->fetchAll(PDO::FETCH_ASSOC) : false;
return $blacklist ? $blacklist : [];
return db_fetch_all($getBlacklist);
}

View file

@ -68,7 +68,6 @@ function user_login_attempts_list(int $offset, int $take, int $userId = 0): arra
$getAttempts->bindValue('offset', $offset);
$getAttempts->bindValue('take', $take);
$attempts = $getAttempts->execute() ? $getAttempts->fetchAll(PDO::FETCH_ASSOC) : false;
return $attempts ? $attempts : [];
return db_fetch_all($getAttempts);
}

View file

@ -79,7 +79,5 @@ function user_relation_info(int $userId, int $subjectId): array
');
$getRelationInfo->bindValue('user_id', $userId);
$getRelationInfo->bindValue('subject_id', $subjectId);
$relationInfo = $getRelationInfo->execute() ? $getRelationInfo->fetch(PDO::FETCH_ASSOC) : false;
return $relationInfo ? $relationInfo : [];
return db_fetch($getRelationInfo);
}

View file

@ -44,8 +44,7 @@ function user_session_find($sessionId, bool $byKey = false): array
WHERE `%s` = :session_id
', $byKey ? 'session_key' : 'session_id'));
$findSession->bindValue('session_id', $sessionId);
$session = $findSession->execute() ? $findSession->fetch(PDO::FETCH_ASSOC) : false;
return $session ? $session : [];
return db_fetch($findSession);
}
function user_session_delete(int $sessionId): void
@ -109,9 +108,8 @@ function user_session_list(int $offset, int $take, int $userId = 0): array
$getSessions->bindValue('offset', $offset);
$getSessions->bindValue('take', $take);
$sessions = $getSessions->execute() ? $getSessions->fetchAll(PDO::FETCH_ASSOC) : false;
return $sessions ? $sessions : [];
return db_fetch_all($getSessions);
}
function user_session_bump_active(int $sessionId): void

View file

@ -158,8 +158,7 @@ function user_warning_fetch(
$fetchWarnings->bindValue('days', $days);
}
$warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false;
return $warnings ? $warnings : [];
return db_fetch_all($fetchWarnings);
}
function user_warning_global_count(?int $userId = null): int
@ -204,8 +203,7 @@ function user_warning_global_fetch(int $offset = 0, int $take = 50, ?int $userId
$fetchWarnings->bindValue('user_id', $userId);
}
$warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false;
return $warnings ? $warnings : [];
return db_fetch_all($fetchWarnings);
}
function user_warning_check_ip(string $address): bool

View file

@ -118,7 +118,5 @@ function audit_log_list(int $offset, int $take, int $userId = 0): array
$getLogs->bindValue('offset', $offset);
$getLogs->bindValue('take', $take);
$logs = $getLogs->execute() ? $getLogs->fetchAll(PDO::FETCH_ASSOC) : false;
return $logs ? $logs : [];
return db_fetch_all($getLogs);
}

View file

@ -87,7 +87,7 @@ function changelog_get_changes(string $date, int $user, int $offset, int $take):
$prep->bindValue('user', $user);
}
return $prep->execute() ? $prep->fetchAll(PDO::FETCH_ASSOC) : [];
return db_fetch_all($prep);
}
define('CHANGELOG_COUNT_QUERY', '

View file

@ -64,8 +64,7 @@ function chat_quotes_single(int $quoteId): array
WHERE `quote_id` = :quote
');
$getSingle->bindValue('quote', $quoteId);
$single = $getSingle->execute() ? $getSingle->fetch(PDO::FETCH_ASSOC) : [];
return $single ? $single : [];
return db_fetch($getSingle);
}
function chat_quotes_parents(int $offset = 0, int $take = MSZ_CHAT_QUOTES_TAKE): array
@ -85,8 +84,7 @@ function chat_quotes_parents(int $offset = 0, int $take = MSZ_CHAT_QUOTES_TAKE):
$getParents->bindValue('offset', $offset);
}
$parents = $getParents->execute() ? $getParents->fetchAll() : [];
return $parents ? $parents : [];
return db_fetch_all($getParents);
}
function chat_quotes_set(int $parentId): array
@ -98,7 +96,7 @@ function chat_quotes_set(int $parentId): array
AND `quote_id` = :parent
');
$getParent->bindValue('parent', $parentId);
$parent = $getParent->execute() ? $getParent->fetch(PDO::FETCH_ASSOC) : [];
$parent = db_fetch($getParent);
return $parent ? array_merge([$parent], chat_quotes_children($parent['quote_id'])) : [];
}
@ -110,8 +108,7 @@ function chat_quotes_children(int $parentId): array
WHERE `quote_parent` = :parent
');
$getChildren->bindValue('parent', $parentId);
$children = $getChildren->execute() ? $getChildren->fetchAll(PDO::FETCH_ASSOC) : [];
return $children ? $children : [];
return db_fetch($getChildren);
}
function chat_quotes_random(): array

View file

@ -47,9 +47,9 @@ function comments_parse_for_display(string $text): string
WHERE `user_id` = :user_id
');
$getInfo->bindValue('user_id', $matches[1]);
$info = $getInfo->execute() ? $getInfo->fetch(PDO::FETCH_ASSOC) : [];
$info = db_fetch($getInfo);
if (!$info) {
if (empty($info)) {
return $matches[0];
}
@ -134,8 +134,7 @@ function comments_votes_get(int $commentId): array
) as `dislikes`
');
$getVotes->bindValue('id', $commentId);
$votes = $getVotes->execute() ? $getVotes->fetch(PDO::FETCH_ASSOC) : false;
return $votes ? $votes : [];
return db_fetch($getVotes);
}
function comments_category_create(string $name): array
@ -193,7 +192,7 @@ function comments_category_info($category, bool $createIfNone = false): array
}
$getCategory->bindValue('category', $category);
$categoryInfo = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false;
$categoryInfo = db_fetch($getCategory);
return $categoryInfo
? $categoryInfo
: (
@ -253,7 +252,7 @@ function comments_category_get(int $category, int $user, ?int $parent = null): a
$getComments->bindValue('user', $user);
$getComments->bindValue('category', $category);
$comments = $getComments->execute() ? $getComments->fetchAll(PDO::FETCH_ASSOC) : [];
$comments = db_fetch_all($getComments);
$commentsCount = count($comments);
for ($i = 0; $i < $commentsCount; $i++) {
@ -319,8 +318,7 @@ function comments_post_get(int $commentId, bool $parse = true): array
WHERE `comment_id` = :id
');
$fetch->bindValue('id', $commentId);
$comment = $fetch->execute() ? $fetch->fetch(PDO::FETCH_ASSOC) : false;
$comment = $comment ? $comment : []; // prevent type errors
$comment = db_fetch($fetch);
if ($comment && $parse) {
$comment['comment_html'] = nl2br(comments_parse_for_display(htmlentities($comment['comment_text'])));
@ -357,5 +355,5 @@ function comments_post_replies(int $commentId): array
WHERE `comment_reply_to` = :id
');
$getComments->bindValue('id', $commentId);
return $getComments->execute() ? $getComments->fetchAll(PDO::FETCH_ASSOC) : [];
return db_fetch_all($getComments);
}

View file

@ -52,7 +52,19 @@ function db_last_insert_id(?string $name = null, ?string $connection = null): st
function db_query_count(?string $connection = null): int
{
return (int)db_query('SHOW SESSION STATUS LIKE "Questions"', $connection)->fetch()['Value'];
return (int)db_query('SHOW SESSION STATUS LIKE "Questions"', $connection)->fetchColumn(1);
}
function db_fetch(PDOStatement $stmt, $default = [])
{
$out = $stmt->execute() ? $stmt->fetch(PDO::FETCH_ASSOC) : false;
return $out ? $out : $default;
}
function db_fetch_all(PDOStatement $stmt, $default = [])
{
$out = $stmt->execute() ? $stmt->fetchAll(PDO::FETCH_ASSOC) : false;
return $out ? $out : $default;
}
// starting at 2

View file

@ -126,8 +126,7 @@ function news_categories_get(
$getCats->bindValue('take', $take);
}
$cats = $getCats->execute() ? $getCats->fetchAll(PDO::FETCH_ASSOC) : false;
return $cats ? $cats : [];
return db_fetch_all($getCats);
}
function news_categories_count(bool $includeHidden = false): int
@ -180,8 +179,7 @@ function news_category_get(
$getCategory = db_prepare($query);
$getCategory->bindValue('category', $category);
$category = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false;
return $category ? $category : [];
return db_fetch($getCategory);
}
function news_posts_count(
@ -263,8 +261,7 @@ function news_posts_get(
$getPosts->bindValue('offset', $offset);
}
$posts = $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : false;
return $posts ? $posts : [];
return db_fetch_all($getPosts);
}
function news_post_comments_set(int $postId, int $sectionId): void
@ -297,9 +294,6 @@ function news_post_get(int $postId): array
ON u.`display_role` = r.`role_id`
WHERE `post_id` = :post_id
');
$getPost->bindValue(':post_id', $postId);
$post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
return $post ? $post : [];
return db_fetch($getPost);
}

View file

@ -96,10 +96,8 @@ function perms_get_role(string $prefix, int $role): int
function perms_get_user_raw(int $user): array
{
$emptyPerms = perms_create();
if ($user < 1) {
return $emptyPerms;
return perms_create();
}
$getPerms = db_prepare(sprintf('
@ -109,15 +107,10 @@ function perms_get_user_raw(int $user): array
AND `role_id` IS NULL
', implode('`, `', perms_get_keys())));
$getPerms->bindValue('user_id', $user);
$perms = db_fetch($getPerms);
if (!$getPerms->execute()) {
return $emptyPerms;
}
$perms = $getPerms->fetch(PDO::FETCH_ASSOC);
if (!$perms) {
return $emptyPerms;
if (empty($perms)) {
return perms_create();
}
return $perms;
@ -125,10 +118,8 @@ function perms_get_user_raw(int $user): array
function perms_get_role_raw(int $role): array
{
$emptyPerms = perms_create();
if ($role < 1) {
return $emptyPerms;
return perms_create();
}
$getPerms = db_prepare(sprintf('
@ -138,15 +129,10 @@ function perms_get_role_raw(int $role): array
AND `role_id` = :role_id
', implode('`, `', perms_get_keys())));
$getPerms->bindValue('role_id', $role);
$perms = db_fetch($getPerms);
if (!$getPerms->execute()) {
return $emptyPerms;
}
$perms = $getPerms->fetch(PDO::FETCH_ASSOC);
if (!$perms) {
return $emptyPerms;
if (empty($perms)) {
return perms_create();
}
return $perms;