dongzi3434 2017-02-12 23:17
浏览 43

使用来自codeigniter控制器的++增加mysql值

I am trying to have my function increment by 1 a value in mysql, but it only works for once - from zero to 1. Then it won't increment. I have noticed that I need to add (int) in order to get it do this, so I guess there is something to do with data type. Do you have any hints?

$mod_seq = $this->db2->query("select mod_sequence from tbl_release_info where release_id = '$release_id';");
$current_modsequence = (int)$mod_seq++;

$update_query_release_info = "update tbl_release_info set mod_sequence = '$current_modsequence' where release_id = '$rid';
  • 写回答

2条回答 默认 最新

  • doudilin1225 2017-02-12 23:25
    关注

    The query in your question returns the number of rows, not the value you want from mod_sequence. Try this instead:

    $query = $this->db2->query("select mod_sequence from tbl_release_info where release_id = '$release_id';");
    
    $row = $query->row();
      if (isset($row)) {
        $mod_seq = $row->mod_sequence;
      }
    $mod_seq++;
    $update_query_release_info = "update tbl_release_info set mod_sequence = '$mod_seq' where release_id = '$rid';
    

    If the datatype of mod_sequence is not an integer, type cast it:

    $mod_seq = (int)$row->mod_sequence; 
    

    Or like this:

    $mod_seq = intval($row->mod_sequence);
    

    EDIT: @RiggsFolly nailed the most elegant solution, by far, in a single query :-) https://stackoverflow.com/a/42194758/1363190

    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?