TDbConnection class
TDbConnection represents a connection to a database.
TDbConnection works together with TDbCommand, TDbDataReader and TDbTransaction to provide data access to various DBMS in a common set of APIs. They are a thin wrapper of the PDO PHP extension.
To establish a connection, set Active to true after specifying ConnectionString, Username and Password.
Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) using the Charset property. The value of this property is database dependant. e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ...
The following example shows how to create a TDbConnection instance and establish the actual connection:
- $connection=new TDbConnection($dsn,$username,$password);
- $connection->Active=true;
After the DB connection is established, one can execute an SQL statement like the following:
- $command=$connection->createCommand($sqlStatement);
- $command->execute(); // a non-query SQL statement execution
- // or execute an SQL query and fetch the result set
- $reader=$command->query();
- // each $row is an array representing a row of data
- foreach($reader as $row) ...
One can do prepared SQL execution and bind parameters to the prepared SQL:
- $command=$connection->createCommand($sqlStatement);
- $command->bindParameter($name1,$value1);
- $command->bindParameter($name2,$value2);
- $command->execute();
To use transaction, do like the following:
- $transaction=$connection->beginTransaction();
- try
- {
- $connection->createCommand($sql1)->execute();
- $connection->createCommand($sql2)->execute();
- //.... other SQL executions
- $transaction->commit();
- }
- catch(Exception $e)
- {
- $transaction->rollBack();
- }
TDbConnection provides a set of methods to support setting and querying of certain DBMS attributes, such as NullConversion.
Located in /Data/TDbConnection.php (line 84)
TComponent | --TDbConnection
Constructor.
Note, the DB connection is not established when this connection instance is created. Set Active property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection
Closes the currently active DB connection.
It does nothing if the connection is already closed.
Creates a command for execution.
Obtains a specific DB connection attribute information.
Returns the ID of the last inserted row or sequence value.
Opens DB connection if it is currently not
Quotes a string for use in a query.
Open or close the DB connection.
Sets an attribute on the database connection.
Close the connection when serializing.
Inherited From TComponent
TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__get()
TComponent::__set()
Documentation generated on Mon, 21 Apr 2008 11:34:52 -0400 by phpDocumentor 1.3.0RC4