itrn0/php-sql-interpolator

A library for interpolating variables into SQL strings in a safe and simple way.

v1.1.0 2023-02-10 08:42 UTC

This package is auto-updated.

Last update: 2024-04-10 11:21:52 UTC


README

PHP SQL Interpolator is a library that allows you to safely interpolate variables into SQL queries. The library is designed to prevent SQL injection.

Installation

You can install the library via composer:

composer require itrn0/php-sql-interpolator

Usage

To use the library, you need to create a new instance of the SqlInterpolator class and use the __invoke method to interpolate variables into your SQL query.

$interp = new SqlInterpolator();
$userId = 1002;
$query = <<<SQL
    SELECT * FROM users WHERE id = {$interp($userId)} 
        OR name IN ({$interp('Alice', 'Bob')})
SQL;
$params = $interp->getParams(); // { ":param_1" => 1002, ... }

// database query example
$db->query($query, $params);