CDbCommand
| Package |
system.db |
| Inheritance |
class CDbCommand »
CComponent |
| Since |
1.0 |
| Version |
$Id: CDbCommand.php 2233 2010-06-28 11:59:26Z qiang.xue $ |
CDbCommand represents an SQL statement to execute against a database.
It is usually created by calling
CDbConnection::createCommand.
The SQL statement to be executed may be set via
Text.
To execute a non-query SQL (such as insert, delete, update), call
execute. To execute an SQL statement that returns result data set
(such as SELECT), use
query or its convenient versions
queryRow,
queryColumn, or
queryScalar.
If an SQL statement returns results (such as a SELECT SQL), the results
can be accessed via the returned
CDbDataReader.
CDbCommand supports SQL statment preparation and parameter binding.
Call
bindParam to bind a PHP variable to a parameter in SQL.
Call
bindValue to bind a value to an SQL parameter.
When binding a parameter, the SQL statement is automatically prepared.
You may also call
prepare to explicitly prepare an SQL statement.
Public Properties
Hide inherited properties
| Property | Type | Description | Defined By |
| connection |
CDbConnection |
the connection associated with this command |
CDbCommand |
| pdoStatement |
PDOStatement |
the underlying PDOStatement for this command
It could be null if the statement is not prepared yet. |
CDbCommand |
| text |
string |
the SQL statement to be executed |
CDbCommand |
Property Details
the connection associated with this command
the underlying PDOStatement for this command
It could be null if the statement is not prepared yet.
the SQL statement to be executed
Method Details
|
|
| $connection |
CDbConnection |
the database connection |
| $text |
string |
the SQL statement to be executed |
Constructor.
Set the statement to null when serializing.
|
public CDbCommand bindParam(mixed $name, mixed $value, int $dataType=NULL, int $length=NULL)
|
| $name |
mixed |
Parameter identifier. For a prepared statement
using named placeholders, this will be a parameter name of
the form :name. For a prepared statement using question mark
placeholders, this will be the 1-indexed position of the parameter. |
| $value |
mixed |
Name of the PHP variable to bind to the SQL statement parameter |
| $dataType |
int |
SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
| $length |
int |
length of the data type |
| {return} |
CDbCommand |
the current command being executed (this is available since version 1.0.8) |
Binds a parameter to the SQL statement to be executed.
|
public CDbCommand bindValue(mixed $name, mixed $value, int $dataType=NULL)
|
| $name |
mixed |
Parameter identifier. For a prepared statement
using named placeholders, this will be a parameter name of
the form :name. For a prepared statement using question mark
placeholders, this will be the 1-indexed position of the parameter. |
| $value |
mixed |
The value to bind to the parameter |
| $dataType |
int |
SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
| {return} |
CDbCommand |
the current command being executed (this is available since version 1.0.8) |
Binds a value to a parameter.
Cancels the execution of the SQL statement.
|
public integer execute(array $params=array (
))
|
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
integer |
number of rows affected by the execution. |
Executes the SQL statement.
This method is meant only for executing non-query SQL statement.
No result set will be returned.
|
|
| {return} |
CDbConnection |
the connection associated with this command |
|
public PDOStatement getPdoStatement()
|
| {return} |
PDOStatement |
the underlying PDOStatement for this command
It could be null if the statement is not prepared yet. |
|
public string getText()
|
| {return} |
string |
the SQL statement to be executed |
Prepares the SQL statement to be executed.
For complex SQL statement that is to be executed multiple times,
this may improve performance.
For SQL statement with binding parameters, this method is invoked
automatically.
|
|
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
CDbDataReader |
the reader object for fetching the query result |
Executes the SQL statement and returns query result.
This method is for executing an SQL query that returns result set.
|
public array queryAll(boolean $fetchAssociative=true, array $params=array (
))
|
| $fetchAssociative |
boolean |
whether each row should be returned as an associated array with
column names as the keys or the array keys are column indexes (0-based). |
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
array |
all rows of the query result. Each array element is an array representing a row.
An empty array is returned if the query results in nothing. |
Executes the SQL statement and returns all rows.
queryColumn()
|
public array queryColumn(array $params=array (
))
|
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
array |
the first column of the query result. Empty array if no result. |
Executes the SQL statement and returns the first column of the result.
This is a convenient method of query when only the first column of data is needed.
Note, the column returned will contain the first element in each row of result.
|
public array queryRow(boolean $fetchAssociative=true, array $params=array (
))
|
| $fetchAssociative |
boolean |
whether the row should be returned as an associated array with
column names as the keys or the array keys are column indexes (0-based). |
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
array |
the first row of the query result, false if no result. |
Executes the SQL statement and returns the first row of the result.
This is a convenient method of query when only the first row of data is needed.
|
public mixed queryScalar(array $params=array (
))
|
| $params |
array |
input parameters (name=>value) for the SQL execution. This is an alternative
to bindParam and bindValue. If you have multiple input parameters, passing
them in this way can improve the performance. Note that you pass parameters in this way,
you cannot bind parameters or values using bindParam or bindValue, and vice versa.
binding methods and the input parameters this way can improve the performance.
This parameter has been available since version 1.0.10. |
| {return} |
mixed |
the value of the first column in the first row of the query result. False is returned if there is no value. |
Executes the SQL statement and returns the value of the first column in the first row of data.
This is a convenient method of query when only a single scalar
value is needed (e.g. obtaining the count of the records).
|
public void setText(string $value)
|
| $value |
string |
the SQL statement to be executed |
Specifies the SQL statement to be executed.
Any previous execution will be terminated or cancel.