2017-02-12 63 views
0

我想让我的函数在mysql中增加一个值,但它只能工作一次 - 从零到1.然后它不会增加。我注意到我需要添加(int)才能完成这个操作,所以我猜这是与数据类型有关的。你有什么提示吗?用++从代码控制器增加mysql值

$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'; 

回答

0

在你的问题中的查询返回的行数,而不是你想从mod_sequence中得到的值。试试这个:

$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'; 

如果mod_sequence的数据类型不是整数,类型强制转换:

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

或者这样:

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

编辑:@RiggsFolly钉最到目前为止,在一个单一的查询优雅的解决方案:-) https://stackoverflow.com/a/42194758/1363190

1

你可以这样做的理由吨SQL和在这样一个单一的查询

$this->db2->query("update tbl_release_info 
        set mod_sequence = mod_sequence + 1 
        where release_id = '$rid'"); 
+0

显然是最简单和最好的解决方案... –

+0

伟大的解决方案!谢谢! – mgeezy