Skip to content
← Back to Functions
Code

Database Health Summary

Summarizes table counts, engine/collation status, index presence, and known risk flags.

Function signature

ogBuildDatabaseHealthSummary(tables = array(), requirements = array())

Categories

  • Database Integrity

Parameters

tablesTable metadata rows.requirementsExpected engine, charset, collation, and required tables. Recognized keys: `charset`, `engine`, `required_tables`.

Return value

Public-safe status string returned by the function.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and procedural mysqli prepared execution where SQL plans are returned; validate file paths, MIME policies, and permissions before file or download workflows.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Builds a safe database health summary from supplied schema/table metadata.
 *
 * This function does not query the database directly and does not expose credentials.
 *
 * @param array $tables Table metadata rows.
 * @param array $requirements Expected engine, charset, collation, and required tables.
 * @return array Database health summary.
 */
function ogBuildDatabaseHealthSummary($tables = array(), $requirements = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($tables)) {
		$result['message'] = 'Table metadata must be an array.';
		return $result;
	}
	if (!is_array($requirements)) {
		$requirements = array();
	}

	$expected_engine = 'InnoDB';
	if (!empty($requirements['engine'])) {
		$expected_engine = (string)$requirements['engine'];
	}
	$expected_charset = 'utf8mb4';
	if (!empty($requirements['charset'])) {
		$expected_charset = (string)$requirements['charset'];
	}

	$warnings = array();
	$summary = array();
	$seen_tables = array();
	foreach ($tables as $table) {
		if (!is_array($table) || empty($table['name'])) {
			continue;
		}
		$name = trim((string)$table['name']);
		$seen_tables[] = $name;
		$engine = '';
		if (!empty($table['engine'])) {
			$engine = (string)$table['engine'];
		}
		$charset = '';
		if (!empty($table['charset'])) {
			$charset = (string)$table['charset'];
		}

		if (!empty($engine) && strcasecmp($engine, $expected_engine) != 0) {
			$warnings[] = $name . ' engine is ' . $engine . ', expected ' . $expected_engine . '.';
		}
		if (!empty($charset) && strcasecmp($charset, $expected_charset) != 0) {
			$warnings[] = $name . ' charset is ' . $charset . ', expected ' . $expected_charset . '.';
		}

		$row_count = 0;
		if (isset($table['row_count'])) {
			$row_count = (int)$table['row_count'];
		}
		$summary[] = array(
			'name' => $name,
			'engine' => $engine,
			'charset' => $charset,
			'row_count' => $row_count
		);
	}

	if (!empty($requirements['required_tables']) && is_array($requirements['required_tables'])) {
		foreach ($requirements['required_tables'] as $required_table) {
			$required_table = (string)$required_table;
			if (!in_array($required_table, $seen_tables, true)) {
				$warnings[] = 'Required table is missing: ' . $required_table;
			}
		}
	}

	$result['success'] = true;
	if (empty($warnings)) {
		$result['message'] = 'Database health summary passed.';
	} else {
		$result['message'] = 'Database health warnings detected.';
	}
	$result['data'] = array(
		'table_count' => count($summary),
		'tables' => $summary,
		'warnings' => $warnings
	);

	return $result;
}