From 5a32abce0e60829f9998d02a0574afa4ed03b3fd Mon Sep 17 00:00:00 2001 From: Joshua Perry <45966243+jpez-development@users.noreply.github.com> Date: Sun, 20 Nov 2022 18:51:17 +0000 Subject: [PATCH] updated query functions --- functions.php | 51 +++++++++++++++++++++++----- public/account/addAuction.php | 10 ++---- public/account/login.php | 7 +--- public/index.php | 20 +++-------- public/listing.php | 63 +++++++++++------------------------ 5 files changed, 69 insertions(+), 82 deletions(-) diff --git a/functions.php b/functions.php index 3d08eb1..56b67a6 100644 --- a/functions.php +++ b/functions.php @@ -3,7 +3,7 @@ function fetchCats() { $pdo = startDB(); $stmt = $pdo->prepare('SELECT * FROM category'); $stmt->execute(); - $cats = $stmt->fetchAll(); + $cats = executeQueryWithoutConstraint('category','*')->fetchAll(); return $cats; } @@ -35,13 +35,7 @@ function checkListing() { } function getListing() { - $pdo = startDB(); - $stmt = $pdo->prepare('SELECT * FROM auction WHERE listing_id = :listing_id'); - $values = [ - 'listing_id' => $_GET['listing_id'] - ]; - $stmt->execute($values); - return $stmt->fetch(); + return getFirstAllMatches('auction', 'listing_id', $_GET['listing_id']); } function populateCatSelect() { @@ -52,4 +46,45 @@ function populateCatSelect() { } return $output; } + +function executeQuery($tableName, $colName, $constraintCol, $constraint) { + $pdo = startDB(); + $stmt = $pdo->prepare('SELECT '. $colName .' FROM '.$tableName.' WHERE '. $constraintCol .' = :constraint'); + $values = [ + 'constraint' => $constraint + ]; + $stmt->execute($values); + return $stmt; +} + +function executeQueryWithoutConstraint($tableName, $colName) { + $pdo = startDB(); + $stmt = $pdo->prepare('SELECT'.$colName.'FROM '.$tableName); + $stmt->execute(); + return $stmt; +} + +function getFirstMatch($tableName, $colName, $constraintCol, $constraint){ + return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetch(); +} + +function getEveryMatch($tableName, $colName, $constraintCol, $constraint){ + return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetchAll(); +} + +function executeAllQuery($tableName, $constraintCol, $constraint) { + return executeQuery($tableName, '*', $constraintCol, $constraint); +} + +function getEveryAllMatches($tableName, $constraintCol, $constraint) { + return executeAllQuery($tableName, $constraintCol, $constraint)->fetchAll(); +} + +function getFirstAllMatches($tableName, $constraintCol, $constraint) { + return executeAllQuery($tableName, $constraintCol, $constraint)->fetch(); +} + + + + ?> \ No newline at end of file diff --git a/public/account/addAuction.php b/public/account/addAuction.php index f3617c8..2c4f669 100644 --- a/public/account/addAuction.php +++ b/public/account/addAuction.php @@ -8,7 +8,6 @@ if (!isset($_SESSION['loggedin'])) { } require_once '../../functions.php'; -$pdo = startDB(); $pageContent = '