From 061291c951f64db1d25b7a29f298f193d9cbe7f9 Mon Sep 17 00:00:00 2001 From: Cyd Date: Wed, 1 Jul 2026 12:51:13 -0500 Subject: [PATCH] Add centralized DB helper; fix legacy mysql_error() landmines - includes/db.php: shared mysqli connection + parameterized query helpers (pqs_db/pqs_exec/pqs_rows/pqs_row/pqs_val), targets PHP 7.4+. - Replace 11 mysql_error() calls (undefined function on PHP 7+) with mysqli_error($link) / mysqli_connect_error() across getFSgame, getFSplayers, config, callsign, console. Only affected already-failed error paths. Co-Authored-By: Claude Opus 4.8 --- callsign.php | 6 ++-- console.php | 6 ++-- getFSgame.php | 2 +- getFSplayers.php | 2 +- includes/config.php | 6 ++-- includes/db.php | 71 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 includes/db.php diff --git a/callsign.php b/callsign.php index 2943a8e..c4e8ff9 100644 --- a/callsign.php +++ b/callsign.php @@ -275,7 +275,7 @@ if (isset($_GET['submit'])) { $link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysqli_error($link)); mysqli_select_db($link, 'pqs') or die('Could not select database'); $query = "SELECT `numpods` FROM `pqs_gameconfig` WHERE 1"; - $result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); + $result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); $line = mysqli_fetch_array($result); $i = 2; $max = intval($line[0]); @@ -292,7 +292,7 @@ if (isset($_GET['submit'])) { $link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysqli_error($link)); mysqli_select_db($link, 'pqs') or die('Could not select database'); $query = "SELECT DISTINCT left(`CallSign`,LOCATE('-',`CallSign`)-1) FROM `pqs_queue` WHERE `CallSign` like '%-%'"; - $result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); + $result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); while ($line = mysqli_fetch_array($result)) { echo " ";} @@ -319,7 +319,7 @@ if (isset($_GET['submit'])) { $link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysqli_error($link)); mysqli_select_db($link, 'pqs') or die('Could not select database'); $query = "SELECT * FROM pqs_mechinfo WHERE `Class` IN ($wclass) ORDER BY mechname"; - $result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); + $result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); while ($line = mysqli_fetch_array($result)) { echo " "; diff --git a/console.php b/console.php index a45a5dc..b91fd2b 100644 --- a/console.php +++ b/console.php @@ -169,10 +169,10 @@ echo "
"; echo "Configuration

"; echo "
"; -$link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysql_error()); +$link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysqli_connect_error()); mysqli_select_db($link, 'pqs') or die('Could not select database'); $query = 'SELECT * FROM pqs_gameconfig'; -$result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); +$result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); while ($line = mysqli_fetch_array($result)) { $numpods = $line[1]; $timepergame = $line[2]; @@ -198,7 +198,7 @@ while ($line = mysqli_fetch_array($result)) { } $query = 'SELECT * FROM pqs_pods'; -$result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); +$result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); while ($line = mysqli_fetch_array($result)) { $pod01 = $line[0]; $pod02 = $line[1]; diff --git a/getFSgame.php b/getFSgame.php index 4e96652..bd9a000 100644 --- a/getFSgame.php +++ b/getFSgame.php @@ -9,7 +9,7 @@ mysqli_select_db($link, 'pqs') or die('Could not select database'); // get current pods $query = 'SELECT * FROM pqs_gameconfig'; -$result = mysqli_query($link, $query) or die('Query failed 12: ' . mysql_error()); +$result = mysqli_query($link, $query) or die('Query failed 12: ' . mysqli_error($link)); $line = mysqli_fetch_array($result); $visibility = $line[7]; diff --git a/getFSplayers.php b/getFSplayers.php index c44e08a..23669bd 100644 --- a/getFSplayers.php +++ b/getFSplayers.php @@ -7,7 +7,7 @@ $link = mysqli_connect('localhost', 'pqs', 'pqs') or die('Could not connect: ' . mysqli_select_db($link, 'pqs') or die('Could not select database'); // get current pods $query = 'SELECT * FROM pqs_pods'; -$result = mysqli_query($link, $query) or die('Query failed: ' . mysql_error()); +$result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link)); $line = mysqli_fetch_array($result); $pod01 = $line[0]; $pod02 = $line[1]; diff --git a/includes/config.php b/includes/config.php index e07aa66..11df16f 100644 --- a/includes/config.php +++ b/includes/config.php @@ -1,9 +1,9 @@ prepare($sql); + if ($types !== '') { + $stmt->bind_param($types, ...$params); + } + $stmt->execute(); + return $stmt; +} + +/** Run a query and return all rows as associative arrays. */ +function pqs_rows(string $sql, string $types = '', array $params = []): array +{ + $stmt = pqs_exec($sql, $types, $params); + $res = $stmt->get_result(); + $rows = $res ? $res->fetch_all(MYSQLI_ASSOC) : []; + $stmt->close(); + return $rows; +} + +/** Run a query and return the first row (assoc) or null. */ +function pqs_row(string $sql, string $types = '', array $params = []): ?array +{ + return pqs_rows($sql, $types, $params)[0] ?? null; +} + +/** Run a query and return a single scalar (first column of first row) or null. */ +function pqs_val(string $sql, string $types = '', array $params = []) +{ + $stmt = pqs_exec($sql, $types, $params); + $res = $stmt->get_result(); + $val = ($res && ($r = $res->fetch_row())) ? $r[0] : null; + $stmt->close(); + return $val; +}