dql7588 2015-03-05 14:33
浏览 18
已采纳

MYSQL用户定义@VARIABLE

I am coding a php-angularjs webapp and I am now creating a revision system with procedure and triggers on insert/update.

after the login I need to do:

$sql = 'SET @id_user := ' . (int)$_SESSION['user']['id_user'].';';

it works well if I select it (returns the user id as expected) but later on (1 min later) after a few queries and pages visited, when I update the customer table everything is good except the @user_id var which is null

Question: Is there a way I can set the var for my full session to avoid repeating the SET @var every time I need to log the modifications?

(it also needs to think that I have multiple admins)

  • 写回答

1条回答 默认 最新

  • drblhw5731 2015-03-05 16:29
    关注

    User-defined variables are session-bound in MySQL. So, they exist in context of single connection.

    User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client session are automatically freed when that client exits.

    If you will reuse same connection, then by definition, variables will be same (so, "shared"). But if your request initializes different connections, then you will not be able to access variables from one session in another.

    As a conclusion - user-defined variables were never intended for state transition, it is unsafe and, actually, a side-effect to rely on them even across several queries in context of one session. They might be used to "tune" your query (let's say, enumeration-related issues as sequences are absent in MySQL), but I would not recommend to use them for state transfer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码