2017-08-13 90 views
0

有人可以請指導我如何獲取「pk」的mysql行值到$ response變量,以便腳本能夠自動取消關注用戶。我已經嘗試過這個腳本,並且在提供pk id時它會取消關注,但我想從mysql自動獲取它以便能夠運行它。感謝您提供的任何幫助。Codeigniter獲取MySQL值

這些都是我沒有成功嘗試過的方法:

$this->db->select('pk'); 
$this->model->from(INSTAGRAM_FOLLOW_TB); 
$this->db->where("id = '11'"); 
$query1 = $this->db->get(); 
$result = $query1->result(); 

主ID,只有取消關注由Mushfik媒體創造了這個ID-,

$response = $i->unfollow($result); 

我也曾嘗試

$accounts = $this->model->fetch("*", INSTAGRAM_ACCOUNT_TB, "id = '11'"); 

$response = $i->unfollow($accounts->pk); 

但沒有奏效。 $ NEED MYSQL數據值是其中值是應該呼應,但並不

case 'unfollow': 
    try { 
     $result = $i->getSelfUsersFollowing(); 

     if(!empty($result) && $result->status == "ok" && !empty($result->users)){ 

      $response = $i->unfollow($NEED MYSQL DATA VALUE); 
      $response = $response->status; 
      $CI =& get_instance(); 
      $CI->load->model('Schedule_model', 'schedule_model'); 
      $lang = $CI->db->insert(INSTAGRAM_FOLLOW_TB, array(
        "pk" => $row->pk, 
        "name" => $row->username, 
        "type"   => $data->schedule_type, 
        "uid"   => $data->uid, 
        "account_id" => $data->account, 
        "account_name" => $data->name, 
          "created"  => NOW 
        )); 
     } 

    } catch (Exception $e){ 

     $response = $e->getMessage(); 
    } 

break; 

回答

0

也許這樣的事情讓你的PK:

$query1 = $this->db->select('pk') 
    ->from(INSTAGRAM_FOLLOW_TB) 
    ->where('id', 11) 
    ->get(); 

if($query1->num_rows() == 1){ 
    $row = $query1->row(); 
    $response = $i->unfollow($row->pk); 
} 

關於你的信息,你要使用$ this當不在對象上下文中時,因爲你在助手中,所以你需要獲取CI超級對象。所以,如果你試圖用$這個 - >分貝,你可以這樣做:

$CI =& get_instance(); 
// Now you can use $CI->db 
// and anything you would normally 
// access via $this in a controller 
// or model 

OK,終於以顯示如何訂購或限制查詢的例子:

$query1 = $this->db->select('pk') 
    ->from(INSTAGRAM_FOLLOW_TB) 
    ->where('id', 11) 
    ->order_by('your_timestamp_field', 'ASC') // This would order by timestamp in ascending order 
    ->limit(1) // This would limit to a single row 
    ->get(); 
+0

謝謝,但實際上並沒有帶來pk值 – user3498121

+0

因爲這是事實,所以需要更多的信息。您可以查看實際的SQL查詢以滿足您的需求: $ this-> db-> last_query(); 我的查詢有一個固定的ID爲11,你可能需要改變。 –

+0

這是在error_log上顯示的錯誤:[13-Aug-2017 22:54:07 Europe/London] PHP致命錯誤:使用$ this而不是在/home/testt/public_html/instagram_helper.php中的對象上下文中811 – user3498121