Query Raw

The queryRaw method executes raw SQL queries and returns full result sets. It is designed for SELECT statements or any SQL that returns rows.

Purpose

The queryRaw method enables direct execution of SQL queries. It retrieves data exactly as returned by the database, making it essential for advanced querying or when ORM features are insufficient.

Parameters

  • $sql — A raw SQL string to execute. Typically a SELECT query or any statement expected to return a result set.

Return Value

Returns an array containing all rows of the result set. Each row is an associative array indexed by column names.

Error Handling

Throws an Exception if the database fails to execute the query. This ensures immediate visibility of SQL errors or unexpected execution issues.

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();