PHP PDO statements

Is there any risk in using integers (and other hardcoded variables) in PDO statments?

ie
$STH=$DBH->prepare("SELECT name FROM Employees WHERE active=1");
$STH->execute();

Seems like it would be a lot of hassle to write it like this:
$STH=$DBH->prepare("SELECT name FROM Employees WHERE active=:active_status");
$STH->execute(array(':active_status'=>1));

(Also, is my syntax ok in the 2nd example?)
jeff_zuckerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Risk from?
The risk comes when you insert external variables into the statement without first sterilising them which is the basis for an injection attack - if you hard code the values the only risk is that the programmer does not know what they are doing.
If your statement will always use a particular (constant) value can't see any benefit in parameterising it.

Syntax looks fine see sample below that uses a cut and paste of your code
<pre>
<?php
$dsn = 'mysql:dbname=ee;host=localhost';
$user = 'user';
$password = 'password';

try {
    $DBH = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$employees = array(
  'Chico', 
  'Harpo', 
  'Groucho', 
  'Gummo', 
  'Zeppo'
);

$query = <<< QUERY
    CREATE TEMPORARY TABLE `Employees` (
       `id` INT NOT NULL, 
       `name` VARCHAR(100), 
       `active` INT
)
QUERY;
$DBH->exec($query);
$k = 1;
foreach($employees as  $id => $emp) {
  $query = "INSERT INTO `Employees` (id, name, active) VALUES ({$id},'{$emp}',{$k})";
  $DBH->exec($query);
  $k = 1 - $k;
}

$STH=$DBH->prepare("SELECT `name` FROM `Employees` WHERE `active`=:active_status");
$STH->execute(array(':active_status'=>1));

$result = $STH->fetchAll();
print_r($result);  
?>

Open in new window

0
Ray PaseurCommented:
This article maps the familiar but obsolete MySQL extension to the MySQLi and PDO extensions, with code examples of "how to" throughout.
http://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Think of query strings as computer programs that drive the behaviors of SQL engine.  If you let non-programmers (or worse, malicious programmers) inject parts of the computer program, you've got a risk.  This is part of the reason why PDO separates the query string from the data and sends them to SQL separately.  If you insert your own data into a query string, you somewhat frustrate the design of PDO that keeps the programming and data separate.

"Is there any risk" requires an answer that paints with a pretty broad brush, so I'll try to answer it from the extremes, then give you a rule that works for me.

1. Let's assume you hardcode data in your script using a literal.  You use this hardcoded data in a query string.  Because PHP cannot reassign literals, you're safe - if it "works" once, it works always.  I put "works" in quotes because there is no guarantee that the query will get you the results set you expect, just that it will be syntactically correct and will be usable by the SQL engine.

2. Let's assume that you need an integer in a query.  If your script contains the literal integer, you're safe.  If your script computes the integer, some level of risk is introduced.  PHP uses type-juggling and what may look like an integer computation to our eyes may result in something other than an integer.  The query will probably still work, just not as you expected, unless you cast the data to integer before injecting it into the SQL statement.

3. Let's assume you need some information for a table lookup, perhaps to find a user-id.  You get this information from the URL.  Now there is more risk because the URL could contain malicious statements that will become part of your SQL program, so data sanitation is an obvious requirement.  But even benign external data can have an adverse effect on SQL query strings.  Consider a search for the client last name.  With 'Zucker' there is no problem.  With 'O'Reilly' there is a problem because the quotation marks are unbalanced by the quotation mark that is part of the name.  MySQL, MySQLi, and PDO handle this issue differently.

The MySQL variants inject the data directly into the SQL query string, surrounded by quotes.  They require the programmer to escape the quotes that are part of the data (not part of the query).  PDO avoids the risk by sending the data separately, in separate PHP variables, so no quotes are required in the PDO query string at all.

I'm not sure what you mean by the oxymoron, "hardcoded variables" but I can tell you this rule is good enough to pass a professional code review.  If there is a  quote mark in a PDO query string, you're doing it wrong.  If there are no quote marks, just the colon-prefixed placeholders, you're probably on firm ground.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jeff_zuckerAuthor Commented:
Two great answers.  Thank you.
0
Ray PaseurCommented:
Glad to help; thanks for using E-E and best of luck with your project, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.