Public Member Functions | |
__construct (Connection $conn, $sql=null) | |
Create a new Query. | |
setSql ($sql) | |
Sets the SQL we are using. | |
setStart ($v) | |
Sets the start row or offset. | |
setMax ($v) | |
Sets max rows (limit). | |
getRows () | |
Gets array of rows (hashes). | |
getRow () | |
Gets first rows (hash). | |
getCol () | |
Gets array of values for first column in result set. | |
getOne () | |
Gets value of first column of first returned row. | |
getAssoc ($scalar=false) | |
Fetch the entire result set of a query and return it as an associative array using the first column as the key. | |
getDataSet () | |
Gets a QueryDataSet representing results of this query. | |
Protected Attributes | |
$conn | |
$sql | |
$max = 0 | |
$start = 0 |
Definition at line 38 of file Query.php.
Query::__construct | ( | Connection $ | conn, | |
$ | sql = null | |||
) |
Query::getAssoc | ( | $ | scalar = false |
) |
Fetch the entire result set of a query and return it as an associative array using the first column as the key.
Note: column names are not preserved when using this function.
For example, if the table 'mytable' contains:
ID TEXT DATE -------------------------------- 1 'one' 944679408 2 'two' 944679408 3 'three' 944679408
$q = new Query("SELECT id, text FROM mytable") $q->getAssoc() returns: array( '1' => array('one'), '2' => array('two'), '3' => array('three'), )
... or call $q->getAssoc($scalar=true) to avoid wrapping results in an array (only applies if only 2 cols are returned): array( '1' => 'one', '2' => 'two', '3' => 'three', )
Keep in mind that database functions in PHP usually return string values for results regardless of the database's internal type.
boolean | $scalar Used only when the query returns exactly two columns. If TRUE, the values of second column are not wrapped in an array. Default here is false, in order to assure expected behavior. |
Definition at line 210 of file Query.php.
00211 { 00212 $stmt = $this->conn->createStatement(); 00213 if ($this->max) $stmt->setLimit($this->max); 00214 if ($this->start) $stmt->setOffset($this->start); 00215 $rs = $stmt->executeQuery($this->sql); 00216 00217 $numcols = null; 00218 $results = array(); 00219 while($rs->next()) { 00220 $fields = $rs->getRow(); 00221 if ($numcols === null) { 00222 $numcols = count($fields); 00223 } 00224 if (!$scalar || ($numcols > 2)) { 00225 $results[ array_shift($fields) ] = array_values($fields); 00226 } else { 00227 $results[ array_shift($fields) ] = array_shift($fields); 00228 } 00229 } 00230 00231 $rs->close(); 00232 $stmt->close(); 00233 00234 return $results; 00235 }
Query::getCol | ( | ) |
Gets array of values for first column in result set.
SQLException |
Definition at line 135 of file Query.php.
00136 { 00137 $stmt = $this->conn->createStatement(); 00138 if ($this->max) $stmt->setLimit($this->max); 00139 if ($this->start) $stmt->setOffset($this->start); 00140 $rs = $stmt->executeQuery($this->sql); 00141 $results = array(); 00142 while($rs->next()) { 00143 $results[] = array_shift($rs->getRow()); 00144 } 00145 $rs->close(); 00146 $stmt->close(); 00147 return $results; 00148 }
Query::getDataSet | ( | ) |
Gets a QueryDataSet representing results of this query.
The QueryDataSet that is returned will be ready to use (records will already have been fetched). Currently only QueryDataSets are returned, so you will not be able to manipulate (update/delete/insert) Record objects in returned DataSet.
$q = new Query("SELECT * FROM author"); $q->setLimit(10); $qds = $q->getDataSet(); foreach($q->getDataSet() as $rec) { $rec->getValue("name"); }
Definition at line 256 of file Query.php.
00257 { 00258 include_once 'jargon/QueryDataSet.php'; 00259 $qds = new QueryDataSet($this->conn, $this->sql); 00260 $qds->fetchRecords($this->start, $this->max); 00261 return $qds; 00262 }
Query::getOne | ( | ) |
Gets value of first column of first returned row.
SQLException |
Definition at line 155 of file Query.php.
00156 { 00157 $stmt = $this->conn->createStatement(); 00158 if ($this->max) $stmt->setLimit($this->max); 00159 if ($this->start) $stmt->setOffset($this->start); 00160 $rs = $stmt->executeQuery($this->sql); 00161 $rs->next(); 00162 $res = array_shift($rs->getRow()); 00163 $rs->close(); 00164 $stmt->close(); 00165 return $res; 00166 }
Query::getRow | ( | ) |
Gets first rows (hash).
Frees resultset.
SQLException |
Definition at line 117 of file Query.php.
00118 { 00119 $stmt = $this->conn->createStatement(); 00120 if ($this->max) $stmt->setLimit($this->max); 00121 if ($this->start) $stmt->setOffset($this->start); 00122 $rs = $stmt->executeQuery($this->sql); 00123 $rs->next(); 00124 $results = $rs->getRow(); 00125 $rs->close(); 00126 $stmt->close(); 00127 return $results; 00128 }
Query::getRows | ( | ) |
Gets array of rows (hashes).
SQLException |
Definition at line 96 of file Query.php.
00097 { 00098 $stmt = $this->conn->createStatement(); 00099 if ($this->max) $stmt->setLimit($this->max); 00100 if ($this->start) $stmt->setOffset($this->start); 00101 $rs = $stmt->executeQuery($this->sql); 00102 $results = array(); 00103 while($rs->next()) { 00104 $results[] = $rs->getRow(); 00105 } 00106 $rs->close(); 00107 $stmt->close(); 00108 return $results; 00109 }
Query::setMax | ( | $ | v | ) |
Query::setSql | ( | $ | sql | ) |
Query::setStart | ( | $ | v | ) |
Query::$sql [protected] |
Definition at line 44 of file Query.php.
Referenced by __construct(), PagedQuery::__construct(), and setSql().