脚本宝典收集整理的这篇文章主要介绍了php – 几个请求同时导致错误的SQL结果,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在中间件中
$user = new User($device_id,$ip);
在用户类中
function __construct($device_id,$ip) { $this->_device_id = $device_id; $this->_ip = $ip; if ($this->isExists()) $this->updateinfo(); else $this->createUser(); } PRivate function isExists() { global $db_core; $selectstatement = $db_core->select(array('id','current_group')) ->From('users') ->where('device_id','=',$this->_device_id); $stmt = $selectStatement->execute(); if ($stmt->rowcount() > 0) { $u = $stmt->fetch(); $this->_id = $u['id']; $this->_current_group = $u['current_group']; return true; } return false; }
createUser()函数在users表中创建一个带有device-id的条目以及其他信息,如date等.
预先感谢您的帮助
INSERT INTO users (...) VALUES(:device_id,:ip,...) ON DUPLICATE KEY UPDATE ip = values(ip),...
我不知道是否可以使用Slim-PDO运行这样的查询,但至少你可以使用通用插入和更新查询,using exceptions,as shown in my article:
$this->_device_id = $device_id; $this->_ip = $ip; try { $this->createUser(); } catch (PDOException $e) { $seArch = "!IntegrITy constraint violation: 1062 Duplicate entry\ .*? for key 'device_id'!"; if (preg_match($search,$e->getMessage())) { $this->updateInfo(); } else { throw $e; } }
以上是脚本宝典为你收集整理的php – 几个请求同时导致错误的SQL结果全部内容,希望文章能够帮你解决php – 几个请求同时导致错误的SQL结果所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。