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.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路