Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the leading colon for parameter names passed to PDOStatement::bindParam() optional?

Tags:

php

pdo

When passing named parameters of the form :name to PDOStatement::bindParam(), it seems to work whether or not the leading colon is used.

i.e. either this:

$statement->bindParam(':name', $var); 

or this:

$statement->bindParam('name', $var); 

seems to work.

Here's the documentation for PDOStatement::bindParam()

parameter

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

Does this mean the colon can be left off?

like image 216
John Carter Avatar asked Mar 19 '12 22:03

John Carter


People also ask

What is the use of bindParam in PHP?

bindParam is a PHP inbuilt function used to bind a parameter to the specified variable name in a sql statement for access the database record. bindValue, on the other hand, is again a PHP inbuilt function used to bind the value of parameter to the specified variable name in sql statement.

What is PDO :: Param_str in PHP?

PDO::PARAM_STR. Represents SQL character data types. For an INOUT parameter, use the bitwise OR operator to append PDO::PARAM_INPUT_OUTPUT to the type of data being bound. Set the fourth parameter, length , to the maximum expected length of the output value.


1 Answers

No, since the documentation doesn't mention this I think it's safe to assume that this behaviour isn't officially supported and shouldn't be relied upon.

However, it does actually happen to work (in PHP 5.3.24 at least) - internally a colon will be added to the parameter if it's missing (see ext/pdo/pdo_stmt.c:363 in the PHP 5.3.24 source code).

like image 189
John Carter Avatar answered Sep 28 '22 18:09

John Carter