Developer's Manual  Function Reference  Database Functions  Class $suSQL  Method queryRow

Method queryRow

Executes query and returns the first row.


array $suSQL->queryRow(string query)
array
$suSQL->cache->queryRow(string query)

queryRow() sends a query to the currently active database on the server and returns the first row as an array or empty array if no records were selected. You can cache query results by calling queryRow() 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 folowing query affects nine rows, but the function returns the first row only.

<?
$query
= "SELECT * FROM test ORDER BY id";
$data = $suSQL->queryRow($query);
print_r($data);

/*
The output of print_r($data) will be:
Array(
    [id] => 1
    [parent] => 0
    [title] => a
)
*/

$query = "SELECT id AS i, parent AS p, title AS t FROM test ORDER BY id";
$data = $suSQL->queryRow($query);
print_r($data);

/*
The output of print_r($data) will be:
Array(
    [i] => 1
    [p] => 0
    [t] => a
)    
*/
?>

The below script shows how to hanlde query result in a correct way.

<?
$query
= "SELECT id AS i, parent AS p, title AS t FROM test WHERE id = 20";
$data = $suSQL->queryRow($query);
if(
$data)
    
print_r($data);
else
    echo
"No records were selected.";
?>
Please login to add comments.