From 3f7669295f2c2ade49ac1104f2bf427072e3ed76 Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Tue, 27 Oct 2026 21:44:21 +0000 Subject: [PATCH] Remove most uses of mysqli_free_result. mysqli_result objects, like all PHP values, are reference counted. When they are overwritten (eg when assigned to in a loop), or when they go out of scope (eg at the end of a block/function), the refcount is decremented, and the object freed when the refcount reaches zero. Thus we can remove mysqli_free_result when it occurs close to the end of a block/function, because it only shortens the lifetime of a result by a tiny amount and clutters up the code for negligible benefit. --- SETUP/MediaWiki_extensions/hospitalExtensions.php | 2 -- SETUP/document_database.php | 2 -- SETUP/tests/unittests/phpunit_test_helpers.inc | 2 -- pinc/CharSuites.inc | 1 - pinc/JsonStorage.inc | 1 - pinc/NonactivatedUser.inc | 2 -- pinc/Project.inc | 5 ----- pinc/RandomRule.inc | 4 ---- pinc/Settings.inc | 3 --- pinc/TallyBoard.inc | 2 -- pinc/Team.inc | 1 - pinc/User.inc | 5 ----- pinc/UserProfile.inc | 3 --- pinc/UserSuggestions.inc | 1 - pinc/forum_interface_phpbb3.inc | 13 ------------- pinc/graph_data.inc | 1 - pinc/showavailablebooks.inc | 3 --- pinc/wordcheck_engine.inc | 9 --------- tasks.php | 1 - tools/project_manager/edit_project_word_lists.php | 3 --- tools/project_manager/show_image_sources.php | 1 - .../show_project_wordcheck_usage.php | 2 -- tools/project_manager/show_word_context.php | 1 - tools/proofers/review_work.php | 1 - .../show_common_words_from_project_word_lists.php | 1 - 25 files changed, 70 deletions(-) diff --git a/SETUP/MediaWiki_extensions/hospitalExtensions.php b/SETUP/MediaWiki_extensions/hospitalExtensions.php index df4c770666..5eb526c887 100644 --- a/SETUP/MediaWiki_extensions/hospitalExtensions.php +++ b/SETUP/MediaWiki_extensions/hospitalExtensions.php @@ -73,7 +73,5 @@ function listHospitalProjects($input, $argv) $output .= "\n"; $output .= "\n"; } - - mysqli_free_result($result); return $output; } diff --git a/SETUP/document_database.php b/SETUP/document_database.php index 626cf4e8b6..c99fc411bc 100755 --- a/SETUP/document_database.php +++ b/SETUP/document_database.php @@ -243,8 +243,6 @@ function query_columns_for_table(string $table_name): array } $rows = mysqli_fetch_all($result, MYSQLI_ASSOC); - mysqli_free_result($result); - return $rows; } diff --git a/SETUP/tests/unittests/phpunit_test_helpers.inc b/SETUP/tests/unittests/phpunit_test_helpers.inc index f4be8dc8a8..cab934cb00 100644 --- a/SETUP/tests/unittests/phpunit_test_helpers.inc +++ b/SETUP/tests/unittests/phpunit_test_helpers.inc @@ -106,8 +106,6 @@ function create_test_image_source(string $image_source): void if (!$result) { throw new Exception(sprintf("Unable to create test image source %s", $image_source)); } - } else { - mysqli_free_result($result); } } diff --git a/pinc/CharSuites.inc b/pinc/CharSuites.inc index 05e98d0b07..a1532fa4f9 100644 --- a/pinc/CharSuites.inc +++ b/pinc/CharSuites.inc @@ -187,7 +187,6 @@ class CharSuites while ($row = mysqli_fetch_assoc($result)) { $charsuites[] = CharSuites::get($row["name"]); } - mysqli_free_result($result); return $charsuites; } diff --git a/pinc/JsonStorage.inc b/pinc/JsonStorage.inc index 5453ff5967..c3bc7e3f61 100644 --- a/pinc/JsonStorage.inc +++ b/pinc/JsonStorage.inc @@ -69,7 +69,6 @@ class JsonStorage while ($row = mysqli_fetch_assoc($result)) { $value = $row['value']; } - mysqli_free_result($result); return $value; } diff --git a/pinc/NonactivatedUser.inc b/pinc/NonactivatedUser.inc index f6ca880918..ec5fb65ae0 100644 --- a/pinc/NonactivatedUser.inc +++ b/pinc/NonactivatedUser.inc @@ -136,8 +136,6 @@ class NonactivatedUser ) ); } - - mysqli_free_result($result); } public function save(): void diff --git a/pinc/Project.inc b/pinc/Project.inc index 356bacd2fe..a66fcddf50 100644 --- a/pinc/Project.inc +++ b/pinc/Project.inc @@ -905,7 +905,6 @@ class Project while ($row = mysqli_fetch_assoc($result)) { $projectids[] = $row['projectid']; } - mysqli_free_result($result); return $projectids; } @@ -1013,7 +1012,6 @@ class Project while ($row = mysqli_fetch_assoc($result)) { $charsuites[] = CharSuites::get($row["charsuite_name"]); } - mysqli_free_result($result); return $charsuites; } @@ -1247,8 +1245,6 @@ class Project while ($row = mysqli_fetch_assoc($res)) { $states[] = $row["state"]; } - mysqli_free_result($res); - return $states; } @@ -2154,7 +2150,6 @@ function load_image_sources(): array while ($row = mysqli_fetch_assoc($result)) { $image_sources[(string)$row['code_name']] = $row; } - mysqli_free_result($result); return $image_sources; } diff --git a/pinc/RandomRule.inc b/pinc/RandomRule.inc index ac0b0988f4..c3d595f567 100644 --- a/pinc/RandomRule.inc +++ b/pinc/RandomRule.inc @@ -79,7 +79,6 @@ class RandomRule $result = DPDatabase::query($sql); $row = mysqli_fetch_assoc($result); - mysqli_free_result($result); if (!$row) { return null; } @@ -106,7 +105,6 @@ class RandomRule ); $result = DPDatabase::query($sql); $row = mysqli_fetch_assoc($result); - mysqli_free_result($result); if (!$row) { return null; } @@ -138,7 +136,6 @@ class RandomRule while ($row = mysqli_fetch_assoc($result)) { $rules[] = new RandomRule($row["id"]); } - mysqli_free_result($result); return $rules; } @@ -168,7 +165,6 @@ class RandomRule 'count' => $row['count'], ]; } - mysqli_free_result($result); return $summary; } diff --git a/pinc/Settings.inc b/pinc/Settings.inc index 35b0e35d4e..f13fff789b 100644 --- a/pinc/Settings.inc +++ b/pinc/Settings.inc @@ -46,7 +46,6 @@ class Settings while ($row = mysqli_fetch_assoc($result)) { $this->_load_value($row['setting'], $row['value']); } - mysqli_free_result($result); } public function UserName(): string @@ -306,8 +305,6 @@ class Settings while ($row = mysqli_fetch_assoc($result)) { $usernames[] = $row['username']; } - mysqli_free_result($result); - return $usernames; } } diff --git a/pinc/TallyBoard.inc b/pinc/TallyBoard.inc index 8aa1929d7c..d7f556429f 100644 --- a/pinc/TallyBoard.inc +++ b/pinc/TallyBoard.inc @@ -325,7 +325,6 @@ class TallyBoard $r++; } - mysqli_free_result($result); // we need to pull the same data from the users table but with a // current_tally of 0 to populate $neighbors @@ -342,7 +341,6 @@ class TallyBoard ); $result = DPDatabase::query($sql); $row = mysqli_fetch_assoc($result); - mysqli_free_result($result); $rank = $ranker->next($row["current_tally"]); $row["current_rank"] = $rank; $neighbors[0] = $row; diff --git a/pinc/Team.inc b/pinc/Team.inc index 17e5d1e48c..fbeca4a20e 100644 --- a/pinc/Team.inc +++ b/pinc/Team.inc @@ -34,7 +34,6 @@ class Team $result = DPDatabase::query($sql); [$count] = mysqli_fetch_row($result); - mysqli_free_result($result); return $count; } } diff --git a/pinc/User.inc b/pinc/User.inc index 9c0cfa7ea9..2f835f5fae 100644 --- a/pinc/User.inc +++ b/pinc/User.inc @@ -291,8 +291,6 @@ class User ) ); } - - mysqli_free_result($result); } public function save(): void @@ -316,7 +314,6 @@ class User while ($row = mysqli_fetch_assoc($result)) { $teams[] = $row["t_id"]; } - mysqli_free_result($result); return $teams; } @@ -536,8 +533,6 @@ class User $is_valid = true; } } - - mysqli_free_result($result); return $is_valid; } diff --git a/pinc/UserProfile.inc b/pinc/UserProfile.inc index fc874163da..c407f5c389 100644 --- a/pinc/UserProfile.inc +++ b/pinc/UserProfile.inc @@ -104,8 +104,6 @@ class UserProfile ); } $this->table_row = mysqli_fetch_assoc($result); - - mysqli_free_result($result); } public function save(): void @@ -184,7 +182,6 @@ class UserProfile while ($row = mysqli_fetch_assoc($result)) { $profiles[] = new UserProfile((int)$row["id"]); } - mysqli_free_result($result); return $profiles; } } diff --git a/pinc/UserSuggestions.inc b/pinc/UserSuggestions.inc index 92f9f9359c..d924ab2d1d 100644 --- a/pinc/UserSuggestions.inc +++ b/pinc/UserSuggestions.inc @@ -443,7 +443,6 @@ class UserSuggestions while ([$count, $value] = mysqli_fetch_row($result)) { $field_counts[$value] = $count; } - mysqli_free_result($result); return $field_counts; } } diff --git a/pinc/forum_interface_phpbb3.inc b/pinc/forum_interface_phpbb3.inc index 7fabded821..0f13514bda 100644 --- a/pinc/forum_interface_phpbb3.inc +++ b/pinc/forum_interface_phpbb3.inc @@ -444,8 +444,6 @@ function get_forum_user_id(string $username): ?int $res = DPDatabase::query($query); $row = mysqli_fetch_assoc($res); - mysqli_free_result($res); - if (!$row) { return null; } @@ -473,8 +471,6 @@ function get_forum_rank_title(int $rank): ?string $res = DPDatabase::query($query); $row = mysqli_fetch_assoc($res); - mysqli_free_result($res); - return $row["rank_title"] ?? null; } @@ -628,8 +624,6 @@ function get_number_of_unread_messages(string $username): ?int $res = DPDatabase::query($query); [$num_messages] = mysqli_fetch_row($res); - mysqli_free_result($res); - return $num_messages; } @@ -650,9 +644,6 @@ function does_topic_exist(int $topic_id): bool $res = DPDatabase::query($query); $exists = (mysqli_num_rows($res) > 0); - - mysqli_free_result($res); - return $exists; } @@ -676,8 +667,6 @@ function get_last_post_time_in_topic(int $topic_id): ?int $res = DPDatabase::query($query); $row = mysqli_fetch_assoc($res); - mysqli_free_result($res); - return $row["max_post_time"] ?? null; } @@ -713,8 +702,6 @@ function get_topic_details(int $topic_id): ?array $res = DPDatabase::query($query); $row = mysqli_fetch_assoc($res); - mysqli_free_result($res); - return $row; } diff --git a/pinc/graph_data.inc b/pinc/graph_data.inc index 493a001fe6..c031456dec 100644 --- a/pinc/graph_data.inc +++ b/pinc/graph_data.inc @@ -776,7 +776,6 @@ function get_round_backlog_stats(array $interested_phases): array while ($result = mysqli_fetch_assoc($res)) { $stats[$phase] = $result["num_pages"] ?? 0; // SELECT SUM() WHERE 0; returns NULL } - mysqli_free_result($res); } // Pop off any trailing phases that have no pages. diff --git a/pinc/showavailablebooks.inc b/pinc/showavailablebooks.inc index b0738dd959..2830e4b9c9 100644 --- a/pinc/showavailablebooks.inc +++ b/pinc/showavailablebooks.inc @@ -588,9 +588,6 @@ function show_project_listing( } echo ""; - - // Free the search results. - mysqli_free_result($result); } // ----------------------------------------------------------------------------- diff --git a/pinc/wordcheck_engine.inc b/pinc/wordcheck_engine.inc index f74e193e37..30053a76ef 100644 --- a/pinc/wordcheck_engine.inc +++ b/pinc/wordcheck_engine.inc @@ -735,9 +735,6 @@ function count_wordcheck_suggestion_events(string $projectid, int $start_time = $res = DPDatabase::query($sql); $row = mysqli_fetch_assoc($res); - - mysqli_free_result($res); - return $row["numevents"]; } @@ -771,9 +768,6 @@ function load_wordcheck_events(string $projectid, int $start_time = 0): array $eventArray[] = [$time, $round, $page, $proofer, $words]; } - - mysqli_free_result($res); - return $eventArray; } @@ -796,9 +790,6 @@ function load_project_good_word_suggestions_flat(string $projectid, int $start_t while ([$suggestions] = mysqli_fetch_row($res)) { $words = array_merge($words, explode("\n", $suggestions)); } - - mysqli_free_result($res); - return $words; } diff --git a/tasks.php b/tasks.php index efdc1688b7..745fef63df 100644 --- a/tasks.php +++ b/tasks.php @@ -1266,7 +1266,6 @@ function load_task(int $tid, bool $is_assoc = true) } else { $task = mysqli_fetch_object($result); } - mysqli_free_result($result); return $task; } diff --git a/tools/project_manager/edit_project_word_lists.php b/tools/project_manager/edit_project_word_lists.php index b90189a468..fdc0ba0ec3 100644 --- a/tools/project_manager/edit_project_word_lists.php +++ b/tools/project_manager/edit_project_word_lists.php @@ -440,9 +440,6 @@ public function number_of_pages_in_round(?Round $round): int $res = DPDatabase::query($sql); $count = mysqli_fetch_row($res); - - mysqli_free_result($res); - return $count[0]; } } diff --git a/tools/project_manager/show_image_sources.php b/tools/project_manager/show_image_sources.php index 083f8bdae5..4d2b1e478b 100644 --- a/tools/project_manager/show_image_sources.php +++ b/tools/project_manager/show_image_sources.php @@ -289,6 +289,5 @@ function load_image_source_stats(): array while ($row = mysqli_fetch_assoc($result)) { $stats[(string)$row['image_source']] = $row; } - mysqli_free_result($result); return $stats; } diff --git a/tools/project_manager/show_project_wordcheck_usage.php b/tools/project_manager/show_project_wordcheck_usage.php index 4d51da03f9..36612bb55b 100644 --- a/tools/project_manager/show_project_wordcheck_usage.php +++ b/tools/project_manager/show_project_wordcheck_usage.php @@ -139,8 +139,6 @@ echo "\n"; } -mysqli_free_result($res); - ?> = $wordInstances) { echo "

" . _("More instances were found; please choose how many to show from the drop-down.") . "

"; diff --git a/tools/proofers/review_work.php b/tools/proofers/review_work.php index d087b29496..f1b3030de4 100644 --- a/tools/proofers/review_work.php +++ b/tools/proofers/review_work.php @@ -437,7 +437,6 @@ function _echo_round_select(array $rounds, ?Round $selected_round): void while ([$image] = mysqli_fetch_row($result)) { $diffLinkString .= "$image "; } - mysqli_free_result($result); } // not sure why the pages that have been saved in the review round diff --git a/tools/site_admin/show_common_words_from_project_word_lists.php b/tools/site_admin/show_common_words_from_project_word_lists.php index 88c1d56e7d..39b0a9198d 100644 --- a/tools/site_admin/show_common_words_from_project_word_lists.php +++ b/tools/site_admin/show_common_words_from_project_word_lists.php @@ -58,7 +58,6 @@ $option_value = urlencode($language); echo ""; } - mysqli_free_result($res); echo ""; echo ""; echo "";