当前位置:网站首页>Php-pdo parameter binding problem

Php-pdo parameter binding problem

2022-06-24 21:12:00 The smell of tobacco

Preface

Today I am executing such a piece of code :

$data = [
    'username' => 'hujingnb',
    'address' => 'beijing',
];
$dbh = new PDO("mysql:host={
      $host};dbname={
      $dbname}", $username, $password);
$statement = $dbh->prepare('INSERT INTO `test_user` (`username`, `address`) VALUES (:username, :address)');
foreach($data as $key => $value ){
    
    $statement->bindParam($key, $value);
}
$ret = $statement->execute();

The meaning is obvious . however , When I see the inserted data content , I was dumbfounded. .

image-20211002152751240

what ? Why is that? , Altogether 5 Line code , There is something wrong with what I wrote ?

To reassure

Final , The compiler saved me . I saw this :

image-20211002152944648

Pay attention to the places in my circle , in other words , This method takes a referenced value . This reminds me of the same pit I encountered before , See my article for details : PHP The problem of circular quotation

Simply speaking , Namely bindParam Method , Receive $value The address of the variable is stored , Every time I cycle, I get the same address . Wait until the end of the cycle , $value The specified value is the last of the array , here , statement Object $value Naturally, it is the last one in the array . The inserted data are all ’beijing’ No wonder .

solve

Now that I know the problem lies in the address reference , The solution lies in the reference of address . How to solve it ?

1. unset

At the end of each cycle , All call unset Method , take $value Release tone , So the next time you enter the loop is , Another new variable .

foreach($data as $key => $value ){
    
    $statement->bindParam($key, $value);
    unset($value);
}

2. Remove the traversal

Don't use foreach, Assign values directly , In this way, there is no intermediate variable problem .

$statement->bindValue(":username", $data['username']);
$statement->bindValue(":address", $data['address']);

3. Use reference to connect

stay foreach in , Connect directly by reference $value The content of , This received itself , It's data value Reference object for , Every $value Your address will naturally be different .

foreach($data as $key => &$value ){
    
    $statement->bindParam($key, $value);
}

4. change to the use of sth. bindValue

and bindParams Methods with similar functions , One more bindValue. This method receives a value variable , Do not receive address .

Think about our functional situation , The official idea is to use bindValue Methods! , bindParams The method doesn't think of any usage scenarios for the time being .

5. Pass parameters directly to execute

execute You can also receive parameter bindings . so , We can also pass the binding of parameters directly to it , No adjustment in the middle bind The method .

$ret = $statement->execute($data);

Last , bindParam Method , Obviously hope sql After it has been created , The value will be acquired later . But I didn't think of any specific application scenarios .

In the end , For the method referenced in this parameter , stay foreach Be careful when calling in .

原网站

版权声明
本文为[The smell of tobacco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211318416133.html