douzhong5902 2015-09-16 20:11
浏览 33
已采纳

用数据库值进行基本数学运算

Completely suprised how I don't know how this should be done.

All I need is to add 1 to database value (not to show it as [0, 1] but literally add (+) 1)

Im using WordPress but this should make no difference.

Functions Im using:

update_user_meta($user_id, $meta_key, $meta_value);

My logic says that I need to retrieve the value, add it & then update but isn't there an easier way?

How this would look:

//Get the value from database
$meta_value = get_user_meta($user_id, $meta_key);
//Math
$meta_value = $meta_value + 1;
//Update the value in database
update_user_meta($user_id, $meta_key, $meta_value);
  • 写回答

1条回答 默认 最新

  • duanlongling5308 2015-09-16 20:23
    关注

    You can easily do a single step one:

    update_user_meta($user_id, $meta_key, intval(get_user_meta($user_id, $meta_key)) + 1);
    

    The intval function gets the integral value of the expression and adding 1 to it will set it accordingly. Hope this helps.

    Or, if you would like to directly deal with the database, you can do something like this, but beware, it has to get the correct database substitutions like wp_, etc:

    UPDATE `wp_usermeta`
      SET `meta_value` = `meta_value` + 1
      WHERE `user_id` = $user_id
      AND `meta_key` = $meta_value;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败