2013-05-15 66 views
1

我写了一个功能,它的工作,除了一个部分:笨 - 无法查询数据库

//Find current upload total value 
$this->EE->db->select('upload_total'); 
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id)); 
return $upload_total->result(); 

// If upload total is more than 0  
if($upload_total > 0) { 
    $new_total = $upload_total + $my_data['upload_total']; 
    $my_data['upload_total'] = $new_total; 
} 

谁能告诉我,如果他们能看到的任何问题的代码的这一特定部分?

这是完整的功能。它运行在一个扩展。该扩展正在工作,它似乎是上面的代码,没有。

我基本上只是试图找到在数据库中的值,然后如果该值是多个,将值添加到另一个。

function cartthrob_on_authorize() 
{ 
    foreach ($this->EE->cartthrob->cart->items() as $item) { 

     // Create array to store member id and purchased upload slots 
     $my_data = array(
      'member_id' => $this->EE->session->userdata('member_id'), 
      'upload_total' => $item->item_options('fees'), 
     ); 

     // Store member id in variable 
     $member_id = $my_data['member_id']; 

     // Query table to check if there is record with member id exists 
     $query = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id)); 

     // If query returns more than 0 update record 
     if($query->num_rows() > 0) { 

      //Find current upload total value 
      $this->EE->db->select('upload_total'); 
      $upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id)); 

      // If upload total is more than 0  
      if($upload_total > 0) { 

       $new_total = $upload_total + $my_data['upload_total']; 

       $my_data['upload_total'] = $new_total; 

      } 

      return $upload_total->result(); 

      $this->EE->db->where('member_id', $member_id); 
      $this->EE->db->update('exp_competition_purchase_upload_total', $my_data); 

     // If query returns 0 insert new record   
     } else { 

      $this->EE->db->insert('exp_competition_purchase_upload_total', $my_data); 

     } 
    } 
} 
+2

是啊你不能运行代码后,你已经返回...'返回$ upload_total->结果();'应该在你的条件语句 – mic

+0

之后完成,当你调用'$ this-> EE-> db'时,你确定你有db的结果吗? – rcpayan

+0

micb:如果我在发生错误后添加它:CI_DB_mysql_result类的对象无法转换为int。 – ccdavies

回答

1
//Find current upload total value 
$this->EE->db->select('upload_total'); 
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id))->result_array(); 
$upload_total = $upload_total[0]; 
//If upload total is more than 0 
$upload_total_col = $upload_total['upload_total']>0 ? $upload_total['upload_total']:0; 
$my_data['upload_total'] += $upload_total_col; 

从我所了解的你,只是想增加upload_total。

注意事项:不要在您的功能之间使用return $upload_total->result();而不提条件。

+0

对于if($ upload_total-> num_rows()> 0){我不想计算行数。我想查看upload_total列中的值是否大于1.我在上面添加了完整的代码,更多解释。 – ccdavies

+0

@ccdavies'$ upload_total-> num_rows()> 0' - >这将检查查询是否已从数据库返回一些值。如果你想检查upload_total列的值,请检查我的编辑... –

+0

谢谢。我只是试了一下,然后返回:调用一个非对象的成员函数num_rows() – ccdavies

0

这里是解决

$this->EE->db->select('upload_total'); 
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total',  array('member_id' => $member_id)); 

// If upload total is more than 0  
if($upload_total->num_rows() > 0) { 
    $result = $upload_total->row_array(); 
    $new_total = $result['upload_total'] + $my_data['upload_total']; 
    $my_data['upload_total'] = $new_total; 
} 
return $my_data; 

尝试。这是因为你试图使用DB返回的对象作为一个int。查询的

+0

p.s.我没有测试它,但它应该沿着正确的路径指向你:) – mic

+0

'$ result + $ my_data ['upload_total'];' - >你试图添加一个具有某个整数值的数组......他也在做同样的错误,只是在他的情况下,它的对象... –

+0

啊斑点大声笑...不是我会错过正常的:P – mic

0

结果不是整数,它是一个对象

尝试这样;

//Find current upload total value 
$this->EE->db->select('upload_total'); 
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id)); 

$result = $upload_total->result(); 

// If upload total is more than 0  
if($upload_total->num_rows() > 0) { 
    $my_data['upload_total'] += $result->upload_total; 
} 

return $my_data; 

顺便说一句,我不知道你想做什么,所以我只是纠正它!