The DB Module

The DB Module provides simple and secure methods for querying, inserting, updating, and managing database records. With built-in support for multiple database connections, prepared statements, debugging tools, and utilities like resequencing IDs and table inspection, it delivers fast, safe, and developer-friendly database interaction without the complexity of heavier ORMs.

Comprehensive guidance for this module can be found in the Database Operations chapter of the Trongate PHP framework documentation.

count()

Counts the total number of records in a specified database table. This method validates table existence, executes a COUNT(*) query, and returns the result as an integer. It provides a simple way to get the total record count from any table.

delete()

Deletes a specific record from a database table based on its ID. This method validates table existence, constructs a parameterized DELETE query, and uses prepared statements to prevent SQL injection. Returns true if the delete operation executes successfully.

describe_table()

Retrieves structural information about a database table using MySQL's DESCRIBE statement. This method can return either detailed column information or just column names. It's useful for dynamic form generation, validation, and understanding table schema at runtime.

get()

Fetches all records from a specified database table with optional ordering and return type. This method validates table existence, prevents SQL injection by validating column names, and supports both object and array return formats.

get_one_where()

Fetches a single record based on a column value from a specified database table. This method validates the table existence and column name, then executes a parameterized query to retrieve the record where the specified column matches the provided value. Returns an object representation of the record if found, or false if no matching record exists.

get_tables()

Retrieves an array of all table names in the current database. This method executes MySQL's SHOW TABLES command and returns the results as a simple indexed array. Useful for database introspection, installation scripts, and administrative tools.

get_where()

Fetches a single record by its ID from a specified database table. This method validates that the table exists, then executes a parameterized query to retrieve the record. Returns an object representation of the record if found, or false if no record matches the ID.

get_where_in()

Retrieves records from a database table where a column's value matches any value in a specified array. This method is useful for fetching multiple records with specific IDs or values in a single query. It validates table existence, uses parameter binding for security, and supports custom column selection and return types.

insert()

Inserts a single record into a specified database table and returns the ID of the newly inserted record. This method validates table existence, constructs a parameterized INSERT query from an associative array, and uses prepared statements to prevent SQL injection. Returns the auto-generated ID from the last insert operation.

insert_batch()

Inserts multiple records into a specified database table using a single batch INSERT statement for improved performance. This method is optimized for inserting large datasets efficiently. It validates table existence, constructs a parameterized batch query, and returns the number of successfully inserted records.

query()

Executes a raw SQL query without parameter binding. This method is for executing SQL statements that don't require external parameter values. Use query_bind() for queries that need parameter binding. The method supports returning results as objects or arrays, or returning null for non-SELECT queries.

query_bind()

Executes a raw SQL query with named parameter binding for maximum security. This is the required methodology for executing SQL queries with dynamic values in Trongate v2. It uses prepared statements with named parameters to prevent SQL injection attacks. Supports returning results as objects or arrays, or returning null for non-SELECT queries.

table_exists()

Checks whether a specified table exists in the current database. This method uses MySQL's SHOW TABLES LIKE statement to verify table existence. Returns true if the table exists, false otherwise. Useful for conditional operations and preventing errors when working with dynamic table names.

update()

Updates a specific record in a database table identified by its ID. This method validates table existence, constructs a parameterized UPDATE query from an associative array of column-value pairs, and uses prepared statements to prevent SQL injection. Returns true if the update operation was successful.