webservco / database-legacy
A PHP component/library. Static mysqli helper for moving old ext/mysql code to PHP 8 without rewriting it.
v2.3.0
2026-10-11 16:34 UTC
Requires
- php: ^8.4
- ext-mysqli: *
- webservco/configuration-legacy: ^2
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^6
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^4
- vimeo/psalm: ^6
- webservco/coding-standards: ^2
- webservco/component-common: ^2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP component/library. Please see webservco/sandbox for an example project using these types of components.
Static mysqli helper for moving old ext/mysql code to PHP 8 without rewriting it.
- Requires:
ext-mysqli, database settings loaded withwebservco/configuration-legacy - Related:
webservco/database(PDO, for new code)
Installation
composer require webservco/database-legacy
Usage
Load the configuration first (see webservco/configuration-legacy). The connection opens on the first query.
use WebServCo\Database\Service\Legacy\Procedural\Db; $result = Db::query( sprintf("SELECT something FROM somewhere WHERE id = '%s'", Db::escape($validatedInput)), ); $something = Db::result($result, 0, 0);
Contracts
| Interface | Implementation | Purpose |
|---|---|---|
| none | Service\Legacy\Procedural\Db |
Static replacements for mysql_* functions, plus transactions. |
Namespaces are relative to WebServCo\Database\.
Configuration
[Database] DB_HOST = 127.0.0.1 ; Optional, defaults to 3306. DB_PORT = 3306 DB_USER = db DB_PASSWORD = db DB_NAME = db
Notes
Convert ext/mysql calls to the Db helper
Each command finds the PHP files under public that use the function and rewrites the calls. It needs ag (The Silver Searcher). Commit or back up before running them.
# mysql_query ag --php -l -Q 'mysql_query(' public | xargs sed -i -e "s#mysql_query(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::query(#g" # mysql_escape_string ag --php -l -Q 'mysql_escape_string(' public | xargs sed -i -e "s#mysql_escape_string(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::escape(#g" # mysql_fetch_array ag --php -l -Q 'mysql_fetch_array(' public | xargs sed -i -e "s#mysql_fetch_array(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::fetchRow(#g" # mysql_fetch_assoc ag --php -l -Q 'mysql_fetch_assoc(' public | xargs sed -i -e "s#mysql_fetch_assoc(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::fetchRow(#g" # mysql_insert_id ag --php -l -Q 'mysql_insert_id(' public | xargs sed -i -e "s#mysql_insert_id(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::insertId(#g" # mysql_num_rows ag --php -l -Q 'mysql_num_rows(' public | xargs sed -i -e "s#mysql_num_rows(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::numRows(#g" # mysql_result ag --php -l -Q 'mysql_result(' public | xargs sed -i -e "s#mysql_result(#\\\WebServCo\\\Database\\\Service\\\Legacy\\\Procedural\\\Db::result(#g"
Development
# Start project. ddev start # Install dependencies. ddev c u # Run all validations. ddev c c # Run unit tests. ddev c test:dox