DHTMLSQL is a zero-setup, lightweight PHP library that wraps PHP's mysqli extension with a safer, more ergonomic API. Its primary deliverable is a single class file. The repository also ships a shared connection bootstrap and a set of standalone demo scripts that serve as living documentation.
- Core library:
library/dhtmlsql.php - Shared DB bootstrap:
config.default.php - Demo index page:
examples/index.php - Demo scripts:
examples/source/*.php - Smoke-test harness:
test.php - Package manifest:
composer.json
This is not an MVC web application. There is no framework, no router, no template engine, and no asset pipeline. The architecture is best understood as a library + demo site.
dhtmlsql/
├── library/
│ └── dhtmlsql.php # Single-file library (the DHTMLSQL class)
├── examples/
│ ├── index.php # HTML navigation page linking to each demo
│ ├── dbtest.sql # MySQL fixture for demo setup
│ └── source/ # Executable demo scripts
│ ├── connect[1-6].php # Connection style examples
│ ├── select.php
│ ├── insert.php
│ ├── insert_bulk.php
│ ├── insert_ignore.php
│ ├── insert_duplicate.php
│ ├── query[1-4].php
│ ├── createtable.php
│ ├── import.php
│ ├── export.php
│ ├── pager1.php
│ ├── pager2.php
│ └── sample.sql # SQL fixture used by import/export demos
├── config.default.php # Shared DB connection bootstrap
├── test.php # Minimal smoke test
├── composer.json # Composer package definition
├── LICENCE
└── README.md
There is no framework router or front controller.
The web server maps URLs directly to PHP files on disk (file-based routing):
| URL | File |
|---|---|
/examples/index.php |
Demo landing page |
/examples/source/select.php |
SELECT demo |
/examples/source/insert.php |
INSERT demo |
/examples/source/pager1.php |
Pagination demo |
/examples/source/import.php |
Import demo |
/examples/source/export.php |
Export demo |
| ... | ... |
Each PHP file runs immediately on request.
There are no controller classes. Each script under examples/source/ acts as its own self-contained controller:
Connection demos (connect1.php – connect6.php)
- Demonstrate different instantiation styles:
new DHTMLSQL(),DHTMLSQL::connect(),DHTMLSQL::get()->connect(), connection reuse, and persistent connections. - Each independently requires
library/dhtmlsql.phpand verifies$db->connected().
CRUD demos (select.php, insert.php, insert_bulk.php, insert_ignore.php, insert_duplicate.php, query1–4.php, createtable.php)
- All require
../../config.default.phpfor a ready$dbhandle. - Call shorthand methods (
select,insert,insert_bulk,insert_update,update,del,query) and dump results.
Import/export demos (import.php, export.php)
import.phpcalls$db->import("sample.sql")to replay SQL statements from a file.export.phpcalls$db->export('*', 'sample.sql')to dump the database to a file.
Pagination demos (pager1.php, pager2.php)
- Call
$db->pquery($sql, $items_per_page, $url_param)which automatically slices results using a$_GETvariable and exposes navigation HTML via$db->show_navigation().
Each script follows the same three-step pattern:
- Include shared config (
../../config.default.php). - Call one or more
DHTMLSQLmethods on$db. - Print output directly (
echo,var_dump, or header-triggered download).
All business/data-access logic lives in library/dhtmlsql.php. It is a singleton-capable, chainable class.
| Style | Code |
|---|---|
| Static factory | $db = DHTMLSQL::connect($host, $user, $pass, $db) |
| Static singleton | $db = DHTMLSQL::get() |
| Procedural | $db = new DHTMLSQL(); $db->connect(...) |
Reuse existing mysqli |
DHTMLSQL::connect($existingMysqliObject) |
Connection
connect()/_connect()— open connection; accepts host, user, password, dbname, port, socket, or an existingmysqliobjectconnected()— returns booleanconnect_error()— returns error stringset_charset($charset, $collation)— setsNAMESand collation on the connection
Queries
query($sql, $replacements)— raw query with?placeholder auto-escaping; supports scalar and array replacements (forIN (?))select($columns, $table, $where, $replacements, $order, $limit)— shorthand SELECTdlookup($column, $table, $where, $replacements)— fetch a single value or rowinsert($table, $columns, $ignore)— shorthand INSERT; when$ignoreis an array, delegates toinsert_updateinsert_bulk($table, $columns, $data, $ignore)— multi-row INSERT in a single queryinsert_update($table, $columns, $update)—INSERT … ON DUPLICATE KEY UPDATE; supports theINC(n)/INC(-n)increment shorthandreplace($table, $columns)— shorthandREPLACE INTOupdate($table, $columns, $where, $replacements)— shorthand UPDATE; supportsINC(n)increment shorthanddel($table, $where, $replacements)— shorthand DELETE
Fetch
fetch_assoc()— fetch one row as associative array (falls through tomysqli_resultvia__call)fetch_assoc_all()— fetch all rows as an array of associative arraysfetch_array()— fetch one row as numeric array (via__callfallback)fetch_row()— fetch one row (via__callfallback)reset_result()— rewind internal result pointer
Result metadata
num_rows()/$db->num_rows— row count from last queryaffected_rows()— rows affected by last INSERT/UPDATE/DELETEinsert_id()— auto-increment ID from last INSERTfetch_sql()— returns the last executed SQL string
Utilities
escape($string)—mysqli_real_escape_stringwrapperimplode($pieces)— escape-aware comma-join forINclausestable_exists($table)— SHOW TABLES checktruncate($table)— TRUNCATE shorthandoptimize()— OPTIMIZE TABLE for all tables with overheaddb_prefix_tables($stmt, $prefix)— replace{tablename}tokens with a prefixget_table_status()— SHOW TABLE STATUS wrapper
Import / Export
import($path)— parse and execute a.sqlfile line by lineexport($tables, $file)— dump table structure + data to a file or trigger a browser downloadfetch_table($table)— generate the SQL dump string for a single table
Pagination
pquery($sql, $items_per_page, $pager_variable)— auto-paginating query; reads$_GET[$pager_variable]for current page and slices results withLIMITshow_navigation()— renders HTML pagination links
Developer ergonomics
preview($mode)— whentrue, prints SQL to screen instead of executing (useful in development)session_logging/session_logger— log all executed SQL to a filereset_session_logger()— clear the log filehalt_on_error/print_error— control error behaviorhalt($message)— error handler; delegates to a globalhalt()function if one exists__call($name, $args)— transparent fallback tomysqliconnection ormysqli_resultmethods (enables raw prepared-statement usage)__get($name)— transparent property proxy to result or connection objects (e.g.,$db->host_info,$db->insert_id)
Used by the majority of example scripts via require "../../config.default.php". It:
- Requires
library/dhtmlsql.php. - Connects to the local
dbtestdatabase (localhost / admin / pass). - Calls
$db->set_charset('utf8', 'utf8_general_ci'). - Leaves a
$dbvariable in scope for the including script.
There is no template engine or reusable view layer.
examples/index.phpis a plain HTML page with a minimal inline<style>block. It acts as a navigation hub linking to each demo script.- Demo scripts in
examples/source/render output directly usingechoandvar_dump, or send raw HTTP download headers (Content-Disposition: attachment).
There is no dedicated assets folder, build pipeline, or front-end tooling.
- The only styling is a small inline
<style>block inexamples/index.php(setstext-decorationon anchor tags). - No JavaScript, images, fonts, or CSS files exist in this repository.
Typical lifecycle for a demo endpoint:
HTTP request
└─► examples/source/<script>.php
└─► require ../../config.default.php
└─► require library/dhtmlsql.php (loads DHTMLSQL class)
└─► DHTMLSQL::connect(...) (opens mysqli connection)
└─► $db->set_charset(...)
└─► $db->query() / select() / insert() / pquery() / import() / export()
└─► mysqli::query() (internal)
└─► session_log() (if enabled)
└─► echo / var_dump / header() + echo (response)
| Concern | Detail |
|---|---|
| Composer type | library |
| PHP requirement | >= 5.1.0 |
| PHP extension | mysqli |
| Autoload strategy | autoload.files — includes library/dhtmlsql.php on every request |
| Zero-setup usage | Drop-in: require 'dhtmlsql.php' |
| Packagist slug | dhtml/dhtmlsql |
| MVC layer | This project's equivalent |
|---|---|
| Model / data-access | library/dhtmlsql.php (DHTMLSQL class) |
| Controller | Procedural scripts in examples/source/ |
| View | examples/index.php (HTML nav) + direct echo in scripts |
| Assets | None (inline CSS only) |
| Router | Web server file mapping (no framework router) |