Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing PDO Query changes bound parameter from integer to string

Tags:

sql

php

pdo

Code Sample

$query = $this->db->prepare( $sql );                  // prepare sql
$query->bindParam( 'start', $start, PDO::PARAM_INT ); // bind start
$query->bindParam( 'end', $end, PDO::PARAM_INT );     // bind end
$query->bindParam( 'language', $this->language );     // bind language
$query->bindValue( 'keyword', "%$keyword%" );         // bind keyword

var_dump( $end );
$query->execute();
var_dump( $end );

Output

int 2
string '2' (length=1)

But... if I switch the order of binds...

$query = $this->db->prepare( $sql );                  // prepare sql
$query->bindParam( 'language', $this->language );     // bind language
$query->bindValue( 'keyword', "%$keyword%" );         // bind keyword
$query->bindParam( 'start', $start, PDO::PARAM_INT ); // bind start
$query->bindParam( 'end', $end, PDO::PARAM_INT );     // bind end

var_dump( $end );
$query->execute();
var_dump( $end );

Output

int 2
int 2

PHP Version: 5.3.8 on Windows

Can anyone explain why this is happening?

like image 478
HyderA Avatar asked Jul 17 '12 12:07

HyderA


People also ask

What is PDO parameter binding?

The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name.

What is the difference between bindParam and bindValue?

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?

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.

What PDO execute return?

PDO::exec() executes an SQL statement in a single function call, returning the number of rows affected by the statement. PDO::exec() does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing PDO::query().


2 Answers

Try turning of emulation for preparing statements

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

like image 113
wmarbut Avatar answered Sep 18 '22 15:09

wmarbut


Checked this with PHP 5.3.13 - two versions of your code gives me:

int 2
string '2' (length=1)

Additionally, with using bindValue() instead of bindParam() two versions of code gives me:

int 2
int 2

p.s. I prefer work with bindValue() and not mix it with bindParam(). Using bindParam() doesn't gives any performance improvements. Some people thinks that passing by values and passing by pointer in PHP works as in C/C++, but this is wrong thinking. Using bindParam() may lead to bugs that hard to find them when they occurs.

like image 44
Alexander R. Avatar answered Sep 17 '22 15:09

Alexander R.