PHP wpdb get_var查询返回零

时间:2015-04-02 06:23:47

标签: php json wordpress api wpdb

我有以下代码,它通过服务器端API向数据库添加一些数据。字段order_number应该通过$ OrderNumberNext创建,它由count(*)+ 1作为order_num变量计算。

但是,完成此操作后检查数据库会显示这只计算为0(零)。我应该为此使用不同的函数调用吗?

 function addChartToSet($chartId, $setId){
     $chartWithId = $this->db->get_var($this->db->prepare("select id from chords where chord_id=%s",$chartId));
     $setWithId = $this->db->get_var($this->db->prepare("select id from sets where set_id=%s",$setId));     

     $orderNumberNext = $this->db->get_var($this->db->prepare("select count(*)+1 as `order_num` from `chords_inside_sets` where `set_id`=%s",$setId));

     $this->db->query($this->db->prepare("update sets set `last_updated_time`=NOW() where `set_id`=%s",$setId));
     $this->db->query($this->db->prepare("insert into `chords_inside_sets` set `chord_id`=%s , `set_id`=%s, `order_number`= %s",$chartWithId,$setWithId,$orderNumberNext));

     return array("last_updated_time"=>  $this->getMaxUpdatedTimeForSet($setId), "query"=>  $this->db->last_query);
 }

1 个答案:

答案 0 :(得分:0)

您在获取数据的查询中添加1。它不能工作。首先通过SELECT查询获取计数,然后在下一行添加一个,如下所示:

 $orderNumberNext = $this->db->get_var($this->db->prepare("select count(*) as `order_num` from `chords_inside_sets` where `set_id`=%s",$setId));
$orderNumberNext++;