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?
The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name.
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.
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.
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().
Try turning of emulation for preparing statements
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With