Class TDbConnection
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 setActive Active to true after specifying setConnectionString ConnectionString, setUsername Username and setPassword Password.
Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) using the setCharset 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 getNullConversion NullConversion.
- TComponent
-
TDbConnection
Copyright: Copyright © 2005-2014 PradoSoft
License: http://www.pradosoft.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 3.0
Located at Data/TDbConnection.php
public
|
#
__construct( string $dsn = '', string $username = '', string $password = '', string $charset = '' )
Constructor. Note, the DB connection is not established when this connection instance is created. Set setActive Active property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection |
public
|
|
public static
array
|
|
public
boolean
|
|
public
|
|
protected
|
|
protected
|
#
close( )
Closes the currently active DB connection. It does nothing if the connection is already closed. |
protected
|
|
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
PDO
|
|
public
|
|
public
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
#
getLastInsertID( string $sequenceName = '' )
Returns the ID of the last inserted row or sequence value. |
public
string
|
|
public
string
|
|
public
string
|
|
public
string
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
boolean
|
|
public
|
|
public
boolean
|
|
public
|
|
public
string
|
|
public
string
|
|
public
string
|
|
public
boolean
|
|
public
string
|
|
public
string
|
|
public
integer
|
|
public
mixed
|
|
public
|
string |
DEFAULT_TRANSACTION_CLASS |
'System.Data.TDbTransaction' |
GLOBAL_RAISE_EVENT_LISTENER
|