25
What’s the difference between Pdostatement::bindParam() and Pdostatement::bindValue()?
25
What’s the difference between Pdostatement::bindParam() and Pdostatement::bindValue()?
35
In accordance with rray quoted:
In bindParam() the expected argument is a reference (variable or constant) and cannot be a primitive type like a string or loose number, function/method return. bindValue() can receive references and values as argument.
$stmt->bindParam(':v1', 10); // Inválido
$stmt->bindParam(':v1', getValor()); // Inválido
PDOStatement::execute()
With bindParam, unlike bindValue, the variable is linked with a reference and will only be evaluated at the time when Pdostatement::execute() is called.
BindParam:
$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindParam(':sex', $sex);
$sex = 'female';
$s->execute(); // Executado quando $sex = 'female'
With bindValue:
$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindValue(':sex', $sex);
$sex = 'female';
$s->execute(); // Executado quando $sex = 'male'
8
In the bindParam()
the expected argument is a reference(variable or constant) cannot be a primitive type like a string or loose number, function/method return. bindValue()
can receive references and values as argument, basically this is it.
The code below returns the error:
Fatal error: Cannot pass Parameter 2 by Reference in
$stmt->bindParam(':v1', 10); //inválido
$stmt->bindParam(':v1', getValor()); //inválido
It would be nice to quote an example of when and why to use bindParam
, for example: http://php.net/manual/en/pdostatement.bindparam.php#example-1006 :) +1
@Guillhermenascimento, as well that that I was seeing
It sounds like a good explanation, I believe that if I augment your answer and add an example it will be very good the answer.
I need to test, do not use sp almost, haha in 80% of cases you will use bindValue()
.
More like 90% kk! Yes, until tomorrow :)
I think mine was more or less... I added your answer too..
@Rowaraujo, I’ll give you an edited I just need some time to do the sp, thing I don’t use much.
7
Look at this SELECT
what use, at least here is a real and commonly used example, as requested:
The bindParam always use with variareis if using a value type 'username' in place of the variable will give error pq it does not accept value, and the bindValue is the reverse I use the same value 'nome do usuário'
,1235456
. I understand so bindParam parameters by reference use variable, bindValue even direct values without use of variable. I may not be right at some points but I hope to help clear up a little.
$usuario ='fulano';
$senha = '123456';
$conn = new PDO('mysql:host=localhost;dbname=nome_banco','','');
$query = $conn->prepare("SELECT * FROM usuarios WHERE usuario = :user AND senha = :pass ");
$query->bindParam(':user', $usuario, PDO::PARAM_STR);
#Uso o bindParam quando uso variáveis conforme acima.
$query->bindValue(':pass', sha1($senha), PDO::PARAM_STR);
#E uso bindValue quando vou passar o valor diretamente na classe pdo como está aqui acima.
#o sha1 vai me retornar um valor, como também poderia usar ex: 12345
$query->bindValue(':pass', 12345, PDO::PARAM_STR);
#ou assim.
$query->bindValue(':pass', 'senha', PDO::PARAM_STR);
$query->execute();
$dados = $query->fetch(PDO::FETCH_ASSOC);
This example might make it clearer how to use, I hope you understand.
It’s not very clear, $usuario
was not set, did you forget to add accidentally? could edit?
@Guilhermenascimento bindParam always use with variables if using a value type 'user name' instead of the variable will give error pq it does not accept value, and bindValue is the inverse I use the same value 'user name' , 1235456, if I use a non-test variable but possibly return a pq error. I understand so bindParam parameters by reference uses variable, bindValue values even without reference.
In fact I already know this, what I mean is that your answer is not "clear", although your comment here explain the differences, I believe you should have described this in the answer. Understands?
I don’t see many people comment here about Pdo, I see a lot Ctr+l Ctrl+v and I’ve seen many answers being texts from the internet and I’ve always suffered with it, but I took and I’ve been testing and learning from mistakes.
yes yes I will add the reply :)
I don’t really understand Ctrl+v ? If such texts copied from the internet are in Portuguese, are clear and the answer has the original source, I see no problem in being from the internet. See the @Rowaraujo response is not very descriptive, but the example of it explains well how the reference works, which is one of the reasons for bindParam. Until tomorrow!
I understand but I said it in the sense to my knowledge that I saw several repeated.
bindValue()
accepted variables also.
good to know @rray :)
Browser other questions tagged php pdo
You are not signed in. Login or sign up in order to post.
Good comparative examples. + 1
– Guilherme Nascimento
which advantage in the difference of execute()?
– Thiago