Connecting to your Database
There are two ways to connect to a database:
Automatically Connecting
The "auto connect" feature will load the database library with every page load. To enable "auto connecting", add the word database to the library array, as indicated in the following file:
application/config/autoload.lc
Manually Connecting
If only some of your pages require database connectivity you can manually connect to your database by adding this line of code in any function where it is needed.
get rigLoadDatabase()
If the above function does not contain any information in the first parameter it will connect to the group specified in your database config file. For most people, this is the preferred method of use.
Available Parameters
- The database connection values, passed either as an array or a DSN string.
- TRUE/FALSE (boolean). Whether to return the connection ID (see Connecting to Multiple Databases below).
- TRUE/FALSE (boolean). Whether to enable the Active Record library. Set to TRUE by default.
Manually Connecting to a Database
The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file. Examples:
To choose a specific group from your config file you can do this:
get rigLoadDatabase("groupName")
Where groupName is the name of the connection group from your config file.
To connect manually to a desired database you can pass an array of values:
put "localhost" into tConfig["hostname"]
put "myusername" into tConfig["username"]
put "mypassword" into tConfig["password"]
put "mydatabase" into tConfig["database"]
put "mysql" into tConfig["dbdriver"]
put "" into tConfig["dbprefix"]
put FALSE into tConfig["pconnect"]
put TRUE into tConfig["dbdebug"]
put FALSE into tConfig["cacheon"]
put "" into tConfig["cachedir"]
put "utf8" into tConfig["charset"]
put "utf8_general_ci" into tConfig["dbcollat"]
get rigLoadDatabase(tConfig);
For information on each of these values please see the configuration page.
Or you can submit your database values as a Data Source Name. DSNs must have this prototype:
put "'dbdriver://username:password@hostname/database'" into tDSN
get rigLoadDatabase(tDSN)
To override default config values when connecting with a DSN string, add the config variables as a query string.
put "'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true'" into tDSN
get rigLoadDatabase(tDSN)
Connecting to Multiple Databases
Connecting to more than one database simultaneously is currently not supported.