Documentation

Escaping
in package

SQL escaping and text preparation utilities.

Provides methods for escaping strings for safe SQL queries and preparing text data for database storage.

Tags
since
3.0.0

Table of Contents

Methods

formatValueForSqlOutput()  : string
Format a value for SQL output (e.g., backup files, SQL dumps).
prepareTextdata()  : string
Replace Windows line return ("\r\n") by Linux ones ("\n").
regexpToSqlSyntax()  : string
Convert a regexp pattern to SQL-safe format.
toSqlSyntax()  : string
Prepares a string to be properly recognized as a string by SQL.
toSqlSyntaxNoNull()  : string
Prepares a string to be properly recognized as a string by SQL.
toSqlSyntaxNoTrimNoNull()  : string
Prepares a string to be properly recognized as a string by SQL.
getConnection()  : mysqli
Get the database connection, asserting it's not null.

Methods

formatValueForSqlOutput()

Format a value for SQL output (e.g., backup files, SQL dumps).

public static formatValueForSqlOutput(string|int|float|null $value) : string

Unlike prepared statements which pass values separately from queries, this method produces properly escaped SQL literals for use in generated SQL statements (like INSERT INTO ... VALUES(...)).

Parameters
$value : string|int|float|null

Database value to format

Return values
string

SQL literal: "NULL" for null, "'escaped'" for strings

prepareTextdata()

Replace Windows line return ("\r\n") by Linux ones ("\n").

public static prepareTextdata(string $s) : string
Parameters
$s : string

Input string

Return values
string

Adapted string.

regexpToSqlSyntax()

Convert a regexp pattern to SQL-safe format.

public static regexpToSqlSyntax(string $input) : string
Parameters
$input : string

Regexp pattern

Return values
string

SQL-safe escaped pattern

toSqlSyntax()

Prepares a string to be properly recognized as a string by SQL.

public static toSqlSyntax(string|int|float $data) : string
Parameters
$data : string|int|float

Input data

Return values
string

Properly escaped and trimmed string. "NULL" if the input string is empty.

toSqlSyntaxNoNull()

Prepares a string to be properly recognized as a string by SQL.

public static toSqlSyntaxNoNull(string $data) : string
Parameters
$data : string

Input string

Return values
string

Properly escaped and trimmed string (never NULL)

toSqlSyntaxNoTrimNoNull()

Prepares a string to be properly recognized as a string by SQL.

public static toSqlSyntaxNoTrimNoNull(string $data) : string
Parameters
$data : string

Input string

Return values
string

Properly escaped string (no trim, never NULL)

getConnection()

Get the database connection, asserting it's not null.

private static getConnection() : mysqli
Tags
throws
RuntimeException

If database connection is not initialized

Return values
mysqli

The active database connection


        
On this page

Search results