Parameterized queries with MariaDB

Post Reply
User avatar
reinaldocrespo
Posts: 986
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL
Been thanked: 2 times

Parameterized queries with MariaDB

Post by reinaldocrespo »

Hello Everyone;

Parameterized queries are key to preventing SQL injection in any SQL including MariaDB. However, in MariaDB (and MySQL), the parameterization itself is handled at the level of the client application, not in SQL syntax directly.

Parameterized queries are also a lot faster when executed inside a loop many times, as the server only parses the SQL once.

Here is a sample code using PHP of a parameterized query with MariaDB.

Code: Select all | Expand

$mysqli = new mysqli("localhost", "user", "password", "database");

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username); // 's' means string
$username = "rcrespo";
$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    print_r($row);
}
Does fwh maria class supports parameterized queries?

Thank you.
User avatar
cnavarro
Posts: 6604
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: Parameterized queries with MariaDB

Post by cnavarro »

Reinaldo, try

Code: Select all | Expand

cSql     := "SHOW FULL TABLES IN `?` LIKE ?"
oMysql:Execute( cSql, { npar1, npar2 } )    // Example with two parameters
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Post Reply