From 970cda9f2ebaf62c4d61bcea6c17f71c018b798b Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Mon, 12 Jul 2021 10:46:52 -0400 Subject: [PATCH] Profile and user.php don't immediately crash --- includes/functions.php | 11 ++++------- profile.php | 2 +- user.php | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 96c5348..dcde97a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -263,14 +263,11 @@ function jsonView($paste_id, $p_title, $p_conntent, $p_code) { } -function getTotalPastes($conn, $username) { - $count = 0; - $query = $conn->prepare("SELECT member FROM pastes WHERE member=?"); +function getTotalPastes(PDO $conn, string $username) : int { + $query = $conn->prepare("SELECT COUNT(*) FROM pastes WHERE member = ?"); $query->execute([$username]); - while ($row = $site_info_rows->fetch()) { - $count = $count + 1; - } - return $count; + + return intval($query->fetch(PDO::FETCH_NUM)[0]); } function isValidUsername(string $str) : bool { diff --git a/profile.php b/profile.php index b168ed9..7cd4d12 100644 --- a/profile.php +++ b/profile.php @@ -74,7 +74,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { updatePageViews($conn); -$total_pastes = getTotalPastes($con, $user_username); +$total_pastes = getTotalPastes($conn, $user_username); // Theme require_once('theme/' . $default_theme . '/header.php'); diff --git a/user.php b/user.php index fbef95c..42695c9 100644 --- a/user.php +++ b/user.php @@ -28,7 +28,7 @@ $user_username = trim($_SESSION['username']); if (isset($_GET['user'])) { $profile_username = trim($_GET['user']); - if (!existingUser($con, $profile_username)) { + if (!existingUser($conn, $profile_username)) { // Invalid username header("Location: ../error.php"); die();