views:

1078

answers:

4

This is a similar question to this "When not to use Prepared statements?", but with the "how-to" -part and for PostgreSQL.

I know that I need prepared statements because I make more than one call to my database during one script.

I would like to get concrete examples about the following sentence

Look at typecasting, validating and sanitizing variables and using PDO with prepared statements.

I know what he mean by validating and sanitizing variables. However, I am not completely sure about prepared statements. How do we prepare statements? By filters, that is by sanitizing? Or by some PDO layer? What is the definition of the layer?

What do prepared statements mean in the statement? Please, use concrete examples.

A: 

How do we prepare statements:

You define a query one time, and can called it as often as you like with different values. (eg. in a loop)

$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM shops WHERE name = $1');
$result = pg_execute($dbconn, "my_query", array("Joe's Widgets"));
$result = pg_execute($dbconn, "my_query", array("row two"));
$result = pg_execute($dbconn, "my_query", array("row three"));

see: http://us2.php.net/manual/en/function.pg-execute.php

Rufinus
+1  A: 

What do prepared statements mean in the statement?

From the documentation:

This feature allows commands that will be used repeatedly to be parsed and planned just once, rather than each time they are executed.

See pg_prepare

Example from the page linked above:

<?php
// Connect to a database named "mary"
$dbconn = pg_connect("dbname=mary");

// Prepare a query for execution
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM shops WHERE name = $1');

// Execute the prepared query.  Note that it is not necessary to escape
// the string "Joe's Widgets" in any way
$result = pg_execute($dbconn, "my_query", array("Joe's Widgets"));

// Execute the same prepared query, this time with a different parameter
$result = pg_execute($dbconn, "my_query", array("Clothes Clothes Clothes"));
?>

The MySQL documentation for Prepared Statements nicely answers the following questions:

  • Why use prepared statements?
  • When should you use prepared statements?
karim79
Do your examples have the same functionality as Robot's? They are much clearer to me than Robot's. He seems to use a lot of arrays which make them difficult to read for me.
Masi
@Masi - the examples I provided are from the pg_prepare doc page. They are easier to follow, but I would say @Glass Robot's examples are more real-world (for example, named markers). I suggest you get your hands dirty ASAP. There is no 'one example to rule them all'.
karim79
Your answer does not use PDO. This suggests me that you can use prepared statements without PDO. PDO seems to offer database independence only.
Masi
It seems that `pg_query = pg_prepare + pg_pg_execution - ability_to_run_a_list_vars_to_a_statement`.
Masi
Please, see my reply to your answer above or at http://stackoverflow.com/questions/1247373/to-use-prepared-statements-by-php-for-postgres/1247767#1247767
Masi
+1  A: 

It means it will help you prevent SQL injection attacks by eliminating the need to manually quote the parameters.

Instead of placing a variable into the sql you use a named or question mark marker for which real values will be substituted when the statement is executed.

Definition of PDO from the PHP manual:
'The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.'

See the php manual on PDO and PDO::prepare.

An example of a prepared statement with named markers:

<?php
$pdo = new PDO('pgsql:dbname=example;user=me;password=pass;host=localhost;port=5432');

$sql = "SELECT username, password
FROM users
WHERE username = :username
AND password = :pass";

$sth = $pdo->prepare($sql);
$sth->execute(array(':username' => $_POST['username'], ':pass' => $_POST['password']));
$result = $sth->fetchAll();

An example of a prepared statement with question mark markers:

<?php
$pdo = new PDO('pgsql:dbname=example;user=me;password=pass;host=localhost;port=5432');

$sql = "SELECT username, password
FROM users
WHERE username = ?
AND password = ?";

$sth = $pdo->prepare($sql);
$sth->execute(array($_POST['username'], $_POST['password']));
$result = $sth->fetchAll();
Glass Robot
Your commands seem to select the columns username and password from the table users when, for instance, username=a and password=a. This suggests me that the code is not practical in real life because there is no sense in selecting the two column-names when you know them already. However, if you select the third column according to the two, then your way seems to work. It seems that the user's inputs are processed such that a failure/accident in the execution of the queries with the user's input does not change the original data, so no SQL injection: for instance, stop if fails in preparing...
Masi
A: 

Reply to Karim79's answer

This

$result = pg_prepare($dbconn, "query1", 'SELECT passhash_md5 FROM users WHERE email = $1');

seems to be the same as this

$result = pg_prepare($dbconn, "query1", 'SELECT passhash_md5 FROM users WHERE email = ?');


Conclusion: the use of pg_prepare and pg_execute makes PHP much more efficient, since you do not need to consider sanitizing. It also helps you in the use of PDO.

Masi