0

If we are using php mysqli or PDO wrapper class, do they prevent SQL injection risks ?

E.g.

https://github.com/ezSQL/ezSQL

or

https://github.com/bennettstone/simple-mysqli

Always Use prepared statements and parameterized queries is advised by all experts.

wrapper class is useful for less coding in quick time and also help to reduce repeated coding.

Then how can we use wrapper class along with prepared statements and parameterized queries simultaneously ?

I am confused by this?

e.g.

example from - How can I prevent SQL injection in PHP?

Use prepared statements and parameterized queries

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with $row
}

And with wrapper class, e.g. we use it as

$name = $database->filter($_POST['name']);
$query = $database->get_results("select* from employee where name='$name'");
foreach ($query as $row){
// do something with $row
}

Then where to use wrapper and where to use prepared statements ?

how to use both simultaneously ?

How to achieve sql injection prevention while using wrapper class ?

Community
  • 1
  • 1

2 Answers2

0

Combining parameterization with a wrapper class would be like if you make your wrapper's get_results() method take an optional array of params:

get_results($sql, array $params = null) 

Then the code for that function would use bind_param() with the array. But it's a pain in Mysqli because bind_param() takes varargs. It's ugly. You have to make the array into an array of references, and then use call_user_func_array() to pass the array as varargs to mysqli's bind_param().

I have a solution here: https://stackoverflow.com/a/7383439/20860

But I urge you to use PDO instead. It's much easier for this task because you can just pass your array of params to PDOStatement::execute().

Something like this:

class MyDatabaseWrapper {

    protected $pdo;

    ...

    public function get_results($sql, array $params=null) {
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}
Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

The answer is simple: a good wrapper class always lets you use prepared statements. Otherwise simply don't use it.

Then where to use wrapper and where to use prepared statements ?

Always.

how to use both simultaneously ?

Just use them.

How to achieve sql injection prevention while using wrapper class ?

By using prepared statements offered by a wrapper class.

It means that both outdated wrappers you managed to find so far should never be used. There are other wrappers that offer you both simplicity and safety.

For example, I've got a very simple PDO wrapper. It's just few lines thanks to PDO already being a wrapper, offers you a lot of automation out of the box.

It makes your code even simpler than with those failed wrappers you mentioned:

$query = $database->run("select* from employee where name=?", [$_POST['name']]);
foreach ($query as $row){
// do something with $row
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345