Database Library

concrete5 uses the ADODB Database Library link to connect to its database.

Accessing the Database Object

Developers can access the database object (and perform any ADODB-specific functions to it) from within any functions or classes simply by running the following command:

$db = Loader::db();

Methods

The $db object returned in the above is an ADODB database object. It supports all ADODB library commands.

A few additional database methods are available from a generic concrete5 Database wrapper class. The following methods can be called statically on that class:

Database::getADOSChema()

Returns an ADOSchema object for use with the ADODB Data Dictionary functionality (link to the relevant ADODB data dictionary documentation.) 

Database::setDebug($debug)

If $debug is set to true, all database queries will be displayed within the page. If set to false, they will no longer be displayed.

Database::setLogging($logging)

If $logging is set to true, all database queries will be logged into the adodb_logsql table in your server's database.

Connecting to Other Databases

Developers are able to switch active database throughout a session with a simple command

// This query runs on the original concrete5 database
	$db = Loader::db();
	$db->Execute('select * from TestTable');
	
// Load another database for some reason
	$db = Loader::db( 'newserver', 'newuser', 'newpassword', 'newdatabase', true);			
	$db->Execute('select * from TestTable2');
	
// return to the original db session
	$db = Loader::db(null, null, null, null, true);

Recent Discussions on this Topic

Single Location for db connection info

What is the best practice for storing database connection information? I will have various Controllers connect to an external database and I don't want to include the host/user/password in each controller.php file/function view(). Thanks.

Sub-Domain using existing database for C5 on primary domain

Is there any documentation for installing C5 on sub-domains and sharing a single database with primary domain? When attempting to auto-install C5 on the sub-domain, error message "Conflicting Database Tables Exist" is returned and I am unable to complete…

Error when Attempting to connect to external Db

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'ADODB_mysql::debug' was given in /httpdocs/concrete/libraries/database.php on line 73 [code] $db = Loader::db( DB_SERVER, DB_USERNAME,…

What is the best way to escape database INSERTS?

I'm wondering what the best way to escape database inserts are. For example, with the following input: !@#$%^&*()"' I've tried: [code] $db = Loader::db(); // Need to look into escaping //$grpName = $db->qstr($grpName); //$grpName = htmlspec…