Navigating through the intricacies of database management in Venddor IO is vital for developers. This segment of our guide delves deep into the diverse facets associated with interfacing and manipulating databases within the Venddor IO framework.
Venddor IO handles database connections through a centralized system that ensures efficient resource management. The framework automatically establishes connections when needed and manages connection pooling to optimize performance.
Connection details are stored in the config.php file. For security reasons, it's important to restrict access to this file and use strong, unique passwords for your database.
Venddor IO provides a suite of functions for interacting with the database, making it easier to execute common operations:
db_query(): Executes a raw SQL querydb_get_array(): Retrieves multiple rows as an arraydb_get_row(): Fetches a single rowdb_get_field(): Returns a single field valuedb_get_fields(): Gets an array of field valuesdb_get_hash_array(): Returns an array indexed by a specified fieldTo prevent SQL injection and simplify query building, Venddor IO supports placeholders in SQL queries:
Example using the table prefix placeholder:
$result = db_get_array(
"SELECT * FROM ?:products WHERE product_id > ?i AND status = ?s",
100,
'A'
);
Common placeholders include:
?: - Table prefix placeholder?i - Integer value?s - String value (sanitized)?a - Array (handled appropriately based on context)?u - Update set (for UPDATE queries)?p - Already prepared data (use with caution)For operations that require multiple database changes to be atomic, Venddor IO provides transaction management functions:
db_start_transaction();
try {
// Perform database operations
db_query("INSERT INTO ?:products ...");
db_query("UPDATE ?:product_prices ...");
db_commit_transaction();
} catch (Exception $e) {
db_rollback_transaction();
throw $e;
}
When working with the Venddor IO database system, consider these best practices:
Direct database modification outside of the Venddor IO API can lead to data inconsistencies, broken relationships, and potential security vulnerabilities. Always use the provided functions when possible.