Method queryData
Executes query and returns result as an array.
array $suSQL->queryData(string query [, string field1 [, mixed field2 [, int aggregate]]) array $suSQL->cache->queryData(string query [, string field1 [, mixed field2 [, int aggregate]])
|
queryData() sends a query to the currently active database on the server and returns result as an array
or empty array if no records were selected. You can cache query results by calling queryData()
method via the cache class. The next time your block executes the same query SiteSupra will bring
back results from the cache instead of hitting the database. Refer to Using Cache for more information about
SiteSupra cache.
Examples
The script runs query and returns an array of rows. Each result row is stored in an array offset, starting at offset 0.
<? $query = "SELECT id, title FROM test WHERE parent = 1"; $data = $suSQL->queryData($query); print_r($data);
/* The output of print_r($data) will be: Array ( [0] => Array ( [id] => 2 [title] => b ) [1] => Array ( [id] => 3 [title] => c ) [2] => Array ( [id] => 4 [title] => d ) [3] => Array ( [id] => 5 [title] => e ) ) */ ?>
|
The field1 parameter specifies the field name to use as an array key.
<? $query = "SELECT id, title FROM test WHERE parent = 1"; $data = $suSQL->queryData($query, 'title'); print_r($data);
/* The output of print_r($data) will be: Array ( [b] => Array ( [id] => 2 [title] => b ) [c] => Array ( [id] => 3 [title] => c ) [d] => Array ( [id] => 4 [title] => d ) [e] => Array ( [id] => 5 [title] => e ) ) */ ?>
|
The field1 together with field2 parameter specify the field name to use as an
array key and the field name to be taken as a value.
The query below selects the all free columns, but the function returns only two of them.
<? $query = "SELECT * FROM test WHERE parent = 1"; $data = $suSQL->queryData($query, 'title', 'id'); print_r($data);
/* The output of print_r($data) will be: Array ( [b] => 2 [c] => 3 [d] => 4 [5] => e ) */ ?>
|
Using field1 together with aggregate parameter will group records by the field1.
Specifying field1 value as 'field1|field2' will group results by the field1 first and then by the field2.
<? $query = "SELECT * FROM test"; $data = $suSQL->queryData($query, 'parent', 0, 1); print_r($data);
/* The output of print_r($data) will be: Array ( [0] => Array ( [0] => Array ( [id] => 1 [parent] => 0 [title] => a ) ) [1] => Array ( [0] => Array ( [id] => 2 [parent] => 1 [title] => b ) [1] => Array ( [id] => 3 [parent] => 1 [title] => c ) [2] => Array ( [id] => 4 [parent] => 1 [title] => d ) [3] => Array ( [id] => 5 [parent] => 1 [title] => e ) ) [2] => Array ( [0] => Array ( [id] => 6 [parent] => 2 [title] => f ) [1] => Array ( [id] => 7 [parent] => 2 [title] => g ) [2] => Array ( [id] => 8 [parent] => 2 [title] => h ) [3] => Array ( [id] => 9 [parent] => 2 [title] => i ) ) ) */ ?>
|
Using field1 and field2 together with aggregate parameter will group records by the field1.
<? $query = "SELECT * FROM test"; $data = $suSQL->queryData($query, 'parent', 'id', 1); print_r($data);
/* The output of print_r($data) will be: Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 ) [2] => Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 ) ) */ ?>
|
You can tell queryData() to sort result in the order you specify. In the example below the $data array
contains records id in the order specified by the field2 array.
<? $field2 = Array('7' => 0, '2' => 0, '5' => 0); $query = "SELECT id, title FROM test WHERE id IN(2, 5, 7)"; $data = $suSQL->queryData($query, 'id', $field2); print_r($data);
/* The output of print_r($data) will be: Array ( [7] => Array ( [id] => 7 [title] => g ) [2] => Array ( [id] => 2 [title] => b ) [5] => Array ( [id] => 5 [title] => e ) ) */ ?>
|
The following example shows what is happening if the field2 array contains values different from the values
specified in the query.
<? $field2 = Array('7' => 0, '3' => 0, '2' => 0, '5' => 0); $query = "SELECT id, title FROM test WHERE id IN(1, 2, 5, 7)"; $data = $suSQL->queryData($query, 'id', $field2); print_r($data);
/* The output of print_r($data) will be: Array ( [7] => Array ( [id] => 7 [title] => g ) [3] => 0 [2] => Array ( [id] => 2 [title] => b ) [5] => Array ( [id] => 5 [title] => e ) [1] => Array ( [id] => 1 [title] => a ) ) */ ?>
|
| Please login to add comments. |
|
|