I am confuse between these two functions Bindvalue()
and BindParam()
%
and _
, so be careful when using LIKE
. So i think BindValue()
is not used when we are using LIKE query.LIKE
query BindParam()
is used. Because as i know BindParam can escape these %
and _
.BindValue()
doesn't gives protection against sql injection. I am not sure about this, is it true?friends tell what i mention in these 3 points is right or wrong. i am beginner in PDO so please explain it clearly ..
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.
The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name. This function bound the variables, pass their value as input, and receives the output value, if any, of their associated parameter marker.
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.
There should be no difference in how values are escaped or not escaped. bindParam
differs from bindValue
in that it references the variable, binding the value only when you execute the statement. bindValue
takes the value immediately. To illustrate:
$stmt = $db->prepare('SELECT * FROM `table` WHERE foo = :foo');
$foo = 'foo';
$stmt->bindValue(':foo', $foo);
$foo = 'bar';
$stmt->execute();
The above executes like SELECT * FROM table WHERE foo = 'foo'
;
$stmt = $db->prepare('SELECT * FROM `table` WHERE foo = :foo');
$foo = 'foo';
$stmt->bindParam(':foo', $foo);
$foo = 'bar';
$stmt->execute()
The above executes like SELECT * FROM table WHERE foo = 'bar'
.
It's true that neither cares about _
or %
as special characters, because generally speaking they aren't special characters as far as the syntax is concerned, and the database driver is not able to analyze the context to figure out whether you mean %
to be a wildcard or the actual character "%" in the context of a LIKE
query.
Both protect against SQL injection.
Well, you took it all wrong.
Bindvalue()
and BindParam()
are equal in either way except for the argument type.
Both of them do not escape % and _, which doesn't matter too much. Such escaping affects only reliability of the returned results, not whatever "injections".
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