[英]PHP - Message Queue - PDO bindParam using extract as reference
我遇到了一个问题,让它正常工作正在融化我的大脑。 我想知道是否有人可以指出我正确的方向。
基本上,我正在使用多线程消息队列处理程序将消息值插入数据库。 一切都可以正常工作,即使下面的代码也可以,但是只能在初始插入时使用。 绑定变量后,它们将保持相同的值,并且不会引用$ json对象中的更改。 我尝试了几种不同的方法,但似乎无法获得工作的参考。 任何帮助,将不胜感激!
private function handle_insert($message) {
// declare data
$json = json_decode($message->body);
// prepare the statement
if (!isset($this->statement)) $this->prepare_statement();
// if there are no bindings
if (!$this->binding) {
// extract params
extract(get_object_vars($json), EXTR_REFS);
// loop over data
foreach ($json as $key => $value) {
// bind data
$this->statement->bindParam(":{$key}", $$key, $this->pdo_param($$key)); // using function for defined types
}
// params are bound
$this->binding = true;
}
// execute the statement
$this->statement->execute();
}
我可以在每次插入时都使用bindParam,甚至可以使用bindValue。 但是,bindParam仅绑定一次然后更改值,从而减少了循环的必要性吗?
我认为这是一个有趣的问题,尽管与PDO无关。
您必须考虑提取的变量引用了哪些值。 然后考虑代码中过度设计的程度。
然后将extract()移动到条件之外。
看起来类对象正在维护变量$ this-> binding的状态,您已经在第一次调用中将其设置为
$this->binding = true;
一旦在第一次调用中设置了绑定值,那么以下条件将阻止在后续调用中将新变量进一步绑定到PDO对象。
if (!$this->binding)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.