Kotchasan Framework Documentation
Result System Overview
Result System Overview
Result classes are used to manage query results, supporting both PDO and array-based results.
Structure
Result/
├── ResultInterface.php # Interface for all result types
├── PDOResult.php # For PDO queries (default)
└── ArrayResult.php # For cached resultsResultInterface
The main interface that all result types must implement.
Methods
| Method | Return | Description |
|---|---|---|
fetch() |
object\|array\|null |
Fetch next row |
fetchAll() |
array |
Fetch all rows |
fetchColumn($col) |
mixed |
Fetch single column |
rowCount() |
int |
Number of affected rows |
columnCount() |
int |
Number of columns |
getColumnMeta($col) |
array |
Column metadata |
getResult() |
mixed |
Underlying result object |
isEmpty() |
bool |
Check if empty |
hasMore() |
bool |
Check if more rows exist |
reset() |
bool |
Reset pointer |
close() |
bool |
Close cursor |
count() |
int |
Number of rows (alias of rowCount) |
Usage
fetch() - Fetch row by row
$result = $db->select('*')->from('user')->execute();
while ($row = $result->fetch()) {
echo $row->name;
}fetchAll() - Fetch all
$result = $db->select('*')->from('user')->execute();
$users = $result->fetchAll();
foreach ($users as $user) {
echo $user->name;
}fetchColumn() - Fetch single column
$result = $db->select('COUNT(*)')->from('user')->execute();
$count = $result->fetchColumn();rowCount() - Affected rows
$result = $db->update('user')
->set(['status' => 0])
->where([['active', 0]])
->execute();
echo "Updated: " . $result->rowCount() . " rows";isEmpty() / hasMore()
$result = $db->select('*')->from('user')->execute();
if ($result->isEmpty()) {
echo "No users found";
}
while ($result->hasMore()) {
$user = $result->fetch();
// process
}reset() - Reset pointer
$result = $db->select('*')->from('user')->execute();
// Read first time
$users = $result->fetchAll();
// Reset and read again
$result->reset();
$usersAgain = $result->fetchAll();PDOResult
Implementation for PDO (default).
use Kotchasan\Result\PDOResult;
// Create from PDOStatement
$result = new PDOResult($statement);
$result = new PDOResult($statement, 'array'); // return arrays instead of objectsResult Format
'default'or'object'- Fetch as object (default)'array'- Fetch as associative array
// Object format (default)
$row = $result->fetch();
echo $row->name;
// Array format
$result = new PDOResult($statement, 'array');
$row = $result->fetch();
echo $row['name'];ArrayResult
Implementation for cached results.
use Kotchasan\Result\ArrayResult;
// Create from array
$data = [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane']
];
$result = new ArrayResult($data);
$result = new ArrayResult($data, 'object'); // return objectsAdditional Methods
| Method | Return | Description |
|---|---|---|
first() |
mixed |
Fetch first row |
all() |
array |
Fetch all (does not move pointer) |
getData() |
array |
Get raw data array |
getResultFormat() |
string |
Get result format |
Iterator Support
ArrayResult supports foreach:
$result = new ArrayResult($data);
foreach ($result as $row) {
echo $row['name'];
}Related Classes
- Database - Core database class
- QueryBuilder - Query building