connector::connect
See Also
Syntax
mixed connect(
optional string $parameter1 = '',
optional string $parameter2 = '',
optional string $parameter3 = '',
optional string $parameter4 = ''
)
Other
This page also shows the syntax for the methods:
nConnect() pConnect()
Return Value
The return value from the statement is either a pointer to the database resource, or null if the connection fails
Description
The essential function ADOdb\connector::connect() establishes a connection to a database using parameters provided with the function, using parameters set in the ADOConnectionDefinitions class, or a combination of both. The traditional description of the connection parameters is as follows:
| Parameter 1 | Parameter 2 | Parameter 3 | Parameter 4 |
|---|---|---|---|
| $hostName | $userId | $password | $database |
However the actual values of the parameters for some databases are such that the actual requirements can be confusing. See the Connection Matrix for assistance.
If the connection is made, $db becomes a database connection object that can be used until the script ends. If the connection fails, the value is set to null.
To debug the connection, set the value debug level in the ADOConnectionDefinitions class
Note About Passwords
Passwords stored in either Dataase or DSN parameters predefined prior to connection are stored as public class variables and may b exposed. Passwords passed directly to the ** connect ** statement are discarded after use.
Upgrading From V5
Note that a failed connection results in a null value in V6, instead of false
/* * ADOdb V5 */ $db = newADOConnection('mysqli'); $db->connect();
/* * ADOdb V6 */ $obj = new ADOdb\connector(); $db = $obj->connect();
Example 1 - Stored Connection Parameters
/* * Example using MySQL */ $dbParameters = array( 'host'=>'192.168.86.85', 'user'=>'adodb', 'password'=>'adodb', 'database'=>'employees' ); /* * Instantiate the connection */ $acd = new ADOdb\ADOConnectionDefinitions(); $acd->driver = 'mysqli'; /* * Set the login parameters */ $acd->dbParameters = $dbParameters; /* * Instantiate the connector */ $obj = new ADOdb\connector($acd); /* * Use the connect statement with no parameters */ $db = $obj->connect();
Example 2 - Passed Connection Parameters
/* * Instantiate the connection */ $acd = new ADOdb\ADOConnectionDefinitions(); $acd->driver = 'mysqli'; /* * Instantiate the connector */ $obj = new ADOdb\connector($acd); /* * Use the connect statement with passed parameters */ $db = $obj->connect('192.168.86.85','adodb','adodb','employees');
Example 3 - Hybrid - Stored Connection + Password
$dbParameters = array( 'host'=>'192.168.86.85', 'user'=>'adodb', 'password'=>'', 'database'=>'employees' ); /* * Instantiate the connection */ $acd = new ADOdb\ADOConnectionDefinitions(); $acd->driver = 'mysqli'; /* * Set the login parameters */ $acd->dbParameters = $dbParameters; /* * Instantiate the connector */ $obj = new ADOdb\connector($acd); /* * Use the connect statement with password */ $db = $obj->connect(null,null,'adodb');
Example 4 - DSN Connection
ADOdb Version 6 no longer support “DSN like” connections strings built from ADOdb connection parameters, that is connection parameters declared as DSN parameters when the driver itself does support DSN connections. This refers, in particular, to the mysqli driver.
| Driver | Supports DSN | Note |
|---|---|---|
| mysqli | No | |
| oci8 | Yes | |
| db2 | Yes | |
| PDO | Yes | All Connection Types |
/* * Instantiate the connection */ $acd = new ADOdb\ADOConnectionDefinitions(); $acd->driver = 'db2'; $acd->dsnParameters = 'hostname=192.168.0.30;protocol=tcpip;port=50000;database=SAMPLE;uid=db2inst1;pwd=instance'; $obj = new ADOdb\connector($acd); $db = $obj->connect();
DSN connections support connection passing the password in the ** connect() ** statement.
Example 5 - Hybrid DSN + Password
/* * Instantiate the connection */ $acd = new ADOdb\ADOConnectionDefinitions(); $acd->driver = 'db2'; $acd->dsnParameters = 'hostname=192.168.0.30;protocol=tcpip;port=50000;database=SAMPLE;uid=db2inst1;'; $obj = new ADOdb\connector($acd); $db = $obj->connect(null,null,'instance');
