At times, leveraging data from an auxiliary database or conducting operations like replication and backup becomes essential. Venddor IO offers developers a streamlined approach to link with database hosts distinct from its core-defined default.
External database connections can be useful for scenarios such as connecting to legacy systems, importing data from other platforms, or maintaining separate databases for specific business functions.
To facilitate connections with an external database, you can utilize the following methods:
This method sets up a connection to an external database host.
db_initiate($host, $user, $password, $name, $params = array());
$host - Specifies the database host$user - Indicates the database user$password - Represents the database password$name - Denotes the database name$params - An array containing additional connection parameters:
dbc_name - A local alias to recognize the target databasetable_prefix - A prefix for the tables in the target database, replacing the ?: placeholderThis method allows shifting between different databases once connected.
db_connect_to($params, $name);
$name - Specifies the name of the database$params - An array comprising additional connection parameters:
dbc_name - A local alias to identify the target database[table_prefix] - (Optional) A prefix for the target database table names, which substitutes the ?: placeholder. If not provided, the table_prefix from db_initiate method $params will be used.In some scenarios, you might need to redirect your queries to another database. With Venddor IO's robust functionality, it becomes effortless to target a new database after setting up its connection.
Example: Connecting to a Backup Database
$params = array(
'dbc_name' => 'backup',
'table_prefix' => 'venddor'
);
db_initiate('localhost', 'db_user', 'db_password', 'venddor_backup', $params);
db_connect_to($params, 'venddor_backup');
$data = db_get_array("SELECT * FROM ?:products");
In this example, the $data variable will extract its content from the venddor_products table present in the venddor_backup database on localhost.
This ability empowers developers to seamlessly transition between databases within the Venddor IO framework, ensuring flexibility and effective data management. Whether you're pulling from backup databases or any other secondary sources, Venddor IO ensures smooth operations.
Always ensure proper security measures when connecting to external databases. Use dedicated database users with appropriate permissions and avoid storing credentials in version-controlled files.