【问题标题】:Codeigniter get mysql valueCodeigniter 获取 mysql 值
【发布时间】:2017-08-13 17:00:05
【问题描述】:

有人可以指导我如何将“pk”的mysql 行的值提取到$response 变量,以便能够允许脚本自动取消关注用户。我已经尝试过该脚本,并且在提供 pk id 时它会取消关注,但我想自动从 mysql 获取它以便能够运行它。感谢您提供的任何帮助。

这些是我尝试过的方法,但没有成功:

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

仅取消关注此 ID 的主 ID-由 Mushfik Media 创建,

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

我也试过

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

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

但是没有用。 $NEED MYSQL DATA VALUE 是应该回显该值但不回显的位置

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;

【问题讨论】:

    标签: php mysql codeigniter instagram-api


    【解决方案1】:

    也许像这样来获得你的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 超级对象。因此,如果您尝试使用 $this->db,您可以这样做:

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

    好的,最后展示一个如何排序或限制查询的示例:

    $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();
    

    【讨论】:

    • 谢谢,但还是没有真正带来pk值
    • 既然如此,就需要更多信息。您可以通过以下方式查看实际的 SQL 查询是否满足您的需求: $this->db->last_query();我的查询有一个 11 的固定 ID,您可能需要更改它。
    • 这是 error_log 上显示的错误:[13-Aug-2017 22:54:07 Europe/London] PHP 致命错误:当不在 /home/testt/ 的对象上下文中时使用 $this public_html/instagram_helper.php 在第 811 行
    • 我更新了我的答案,向您展示如何在帮助程序中访问 db。另外,为什么你的助手不在 /application/helpers 里面?
    • 谢谢!它解决了。非常感谢您的时间和精力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多