当前位置:网站首页>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. .
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 :
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 .
边栏推荐
- Limit summary (under update)
- Vant component used in wechat applet
- Popupwindow touch event transparent transmission scheme
- Network security review office starts network security review on HowNet
- Shrimp skin test surface treated
- Common data model (updating)
- Shell script
- C語言實現掃雷(簡易版)
- Self signed certificate generation
- Introduction to interval DP
猜你喜欢
Difference between map and object
Intermediary model -- collaboration among departments
Appium introduction and environment installation
Grating diffraction
A/b test helps the growth of game business
浅谈MySql update会锁定哪些范围的数据
在Dialog中使用透明的【X】叉叉按钮图片
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
Design of routing service for multi Activity Architecture Design
[performance tuning basics] performance tuning strategy
随机推荐
JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
Enjoy yuan mode -- a large number of flying dragons
Summary of message protocol problems
Typescript syntax
Variable setting in postman
Leetcode (146) - LRU cache
Smooth live broadcast | analysis of key technologies for live broadcast pain points
A/b test helps the growth of game business
A/B测试助力游戏业务增长
Prototype mode -- clone monster Army
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
Leetcode(146)——LRU 缓存
Procedural life: a few things you should know when entering the workplace
Tool composition in JMeter
Bean lifecycle flowchart
regular expression
Splicing audio files with ffmpeg-4.3
JUnit unit test
Leetcode(455)——分发饼干