Method Documentation: queryRaw
Purpose
The queryRaw
method is designed for executing raw SQL queries, particularly SELECT statements, or any query where a result set is expected. This method facilitates direct interaction with the database to fetch data, returning an array that contains all the rows in the result set, making it essential for data retrieval operations.
Parameters
string $sql
- The raw SQL query to be executed. This parameter enables the developer to specify the SELECT query or any other query expected to return a result set.
Return Value
The method returns an array consisting of all the rows from the result set. This return structure is particularly useful for processing or displaying data retrieved from the database.
Error Handling
If the database operation encounters any failure, an exception is thrown. This ensures that any issues during the execution of the query are immediately flagged, allowing for prompt error handling and resolution.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$sql = "SELECT * FROM Users WHERE status = 'active'";
try {
$resultSet = $prisma->queryRaw($sql);
foreach ($resultSet as $row) {
echo "User ID: " . $row['id'] . " - Name: " . $row['name'] . "\n";
}
} catch (\Exception $e) {
echo "An error occurred: " . $e->getMessage();
}