$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 {@link getNullConversion NullConversion}.
Показать файл
Открыть проект
Примеры использования класса
Метод | Описание | |
---|---|---|
__construct ( $dsn = '', $username = '', $password = '', $charset = '' ) | Constructor. | |
__sleep ( ) | Close the connection when serializing. | |
beginTransaction ( ) : |
Starts a transaction. | |
createCommand ( $sql ) : |
Creates a command for execution. | |
getActive ( ) : boolean | ||
getAttribute ( $name ) : mixed | Obtains a specific DB connection attribute information. | |
getAutoCommit ( ) : boolean | ||
getAvailableDrivers ( ) : array | ||
getCharset ( ) : string | ||
getClientVersion ( ) : string | ||
getColumnCase ( ) : |
||
getConnectionStatus ( ) : string | ||
getConnectionString ( ) : string | ||
getCurrentTransaction ( ) : |
||
getDbMetaData ( ) : |
||
getDriverName ( ) : string | ||
getLastInsertID ( $sequenceName = '' ) : string | Returns the ID of the last inserted row or sequence value. | |
getNullConversion ( ) : |
||
getPassword ( ) : string | ||
getPdoInstance ( ) : PDO | ||
getPersistent ( ) : boolean | ||
getPrefetch ( ) : boolean | ||
getServerInfo ( ) : string | ||
getServerVersion ( ) : string | ||
getTimeout ( ) : integer | ||
getTransactionClass ( ) : string | ||
getUsername ( ) : string | ||
quoteColumnAlias ( string $name ) : string | Quotes a column alias for use in a query. | |
quoteColumnName ( string $name ) : string | Quotes a column name for use in a query. | |
quoteString ( $str ) : string | Quotes a string for use in a query. | |
quoteTableName ( string $name ) : string | Quotes a table name for use in a query. | |
setActive ( $value ) | Open or close the DB connection. | |
setAttribute ( $name, $value ) | Sets an attribute on the database connection. | |
setAutoCommit ( $value ) | ||
setCharset ( $value ) | ||
setColumnCase ( $value ) | ||
setConnectionString ( $value ) | ||
setNullConversion ( $value ) | ||
setPassword ( $value ) | ||
setPersistent ( $value ) | ||
setTransactionClass ( $value ) | ||
setUsername ( $value ) |
Метод | Описание | |
---|---|---|
close ( ) | Closes the currently active DB connection. | |
open ( ) | Opens DB connection if it is currently not | |
setConnectionCharset ( ) | * Set the database connection charset. |
public __construct ( $dsn = '', $username = '', $password = '', $charset = '' ) |
public beginTransaction ( ) : |
||
Результат | the transaction initiated |
public createCommand ( $sql ) : |
||
Результат | the DB command |
public getAttribute ( $name ) : mixed | ||
Результат | mixed | the corresponding attribute information |
public getAutoCommit ( ) : boolean | ||
Результат | boolean | whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature. |
public static getAvailableDrivers ( ) : array | ||
Результат | array | list of available PDO drivers |
public getCharset ( ) : string | ||
Результат | string | the charset used for database connection. Defaults to emtpy string. |
public getClientVersion ( ) : string | ||
Результат | string | the version information of the DB driver |
public getColumnCase ( ) : |
||
Результат | the case of the column names |
public getConnectionStatus ( ) : string | ||
Результат | string | the status of the connection Some DBMS (such as sqlite) may not support this feature. |
public getConnectionString ( ) : string | ||
Результат | string | The Data Source Name, or DSN, contains the information required to connect to the database. |
public getCurrentTransaction ( ) : |
||
Результат | the currently active transaction. Null if no active transaction. |
public getDbMetaData ( ) : |
||
Результат |
public getDriverName ( ) : string | ||
Результат | string | name of the DB driver |
public getLastInsertID ( $sequenceName = '' ) : string | ||
Результат | string | the row ID of the last row inserted, or the last value retrieved from the sequence object |
public getNullConversion ( ) : |
||
Результат | how the null and empty strings are converted |
public getPassword ( ) : string | ||
Результат | string | the password for establishing DB connection. Defaults to empty string. |
public getPdoInstance ( ) : PDO | ||
Результат | PDO | the PDO instance, null if the connection is not established yet |
public getPersistent ( ) : boolean | ||
Результат | boolean | whether the connection is persistent or not Some DBMS (such as sqlite) may not support this feature. |
public getPrefetch ( ) : boolean | ||
Результат | boolean | whether the connection performs data prefetching |
public getServerInfo ( ) : string | ||
Результат | string | the information of DBMS server |
public getServerVersion ( ) : string | ||
Результат | string | the version information of DBMS server |
public getTimeout ( ) : integer | ||
Результат | integer | timeout settings for the connection |
public getTransactionClass ( ) : string | ||
Результат | string | Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'. |
public getUsername ( ) : string | ||
Результат | string | the username for establishing DB connection. Defaults to empty string. |
public quoteColumnAlias ( string $name ) : string | ||
$name | string | column name |
Результат | string | the properly quoted column alias |
public quoteColumnName ( string $name ) : string | ||
$name | string | column name |
Результат | string | the properly quoted column name |
public quoteString ( $str ) : string | ||
Результат | string | the properly quoted string |
public quoteTableName ( string $name ) : string | ||
$name | string | table name |
Результат | string | the properly quoted table name |
public setAttribute ( $name, $value ) |
protected setConnectionCharset ( ) |