3

I want to bind variables instead of just building SQL strings. Anyway to do this in Php?

Either MySQL or PostgreSQL answers would help.

Thanks

2
  • Binding records from a table to objects in your application or "binding" variables in PHP to variables in SQL? Commented Dec 7, 2009 at 13:59
  • "binding" variables in PHP to variables in SQL :) Commented Dec 7, 2009 at 14:12

4 Answers 4

8

There's e.g. PDO.
An introduction to pdo and prepared statements (including bound parameters) is located at http://docs.php.net/pdo.prepared-statements

Sign up to request clarification or add additional context in comments.

1 Comment

carefull with PDO::bindParam: stackoverflow.com/questions/833510/…
4

You should read on the MySQL Improved Extension (MySQLi) at http://php.net/manual/en/book.mysqli.php , and on prepared statements

Comments

2

For Postgres specifically - pg_query_params (and pg_send_query_params) is the most primitive form of binding but still very useful.

And then there's PDO but the others already mentioned it.

Comments

0

There are a couple of flavors. I believe the more savvy individuals here will push for you to use PDO prepared statements. There is also a sprintf() version.

PDO

An answer has already been discussed on StackOverflow here.

SPRINTF

$sql = sprintf('SELECT * FROM table WHERE id = %d AND field = %s',
               $id,
               mysql_real_escape_string($value));

2 Comments

how sprintf is a bind variables replacemet? there are no DB checks on the variables content. This is as not safe as the plain text stuff
The sprintf version is vulnerable to sql injection and should not be used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.