Execute Raw

The executeRaw method executes raw SQL commands that modify data — such as INSERT, UPDATE, and DELETE — and returns the number of affected rows.

Purpose

The executeRaw method provides direct access for executing SQL commands that perform data modification. It is ideal for situations where ORM-level helpers are not flexible enough or when precise SQL logic is required.

Parameters

  • $sql — A raw SQL statement to execute. Typically an INSERT, UPDATE, or DELETE operation.

Return Value

Returns an integer representing the number of rows affected by the operation. This provides direct insight into how many records were modified.

Error Handling

Throws an Exception if the SQL operation fails. This ensures that database-level errors are visible and can be handled appropriately.

Example Usage

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();

$sql = "UPDATE Users SET status = 'active' WHERE registration_date < '2023-01-01'";

try 
    $affectedRows = $prisma->executeRaw($sql);
    echo "Number of rows affected: " . $affectedRows;
catch (Exception $e) 
    echo "An error occurred: " . $e->getMessage();