将数组转换为字符串MySQL查询

时间:2021-09-01 12:07:07

I have an array which I want to use in a query:

我有一个数组,我想在查询中使用:

Array ( [0] => stdClass Object ( [followee] => 267,269,270,271,272,273,275,276,277,278,279 ) )

I'm not sure why it returns the message:

我不确定为什么它会返回消息:

Object of class stdClass could not be converted to string.

类stdClass的对象无法转换为字符串。

Controller:

$data['activity'] = $this->home_model->activity($followID);

Model:

function activity($followID)
{
    $query_str = 'SELECT DISTINCT name
                    FROM place
                    WHERE userid IN ("'. implode('","', $followID) .'")';

$query = $this->db->query($query_str);

2 个解决方案

#1


2  

Try $your_array[0]->followee.

#2


0  

Well, you have an object in you variable and you don't want to convert that.

好吧,你有一个变量对象,你不想转换它。

The right way, to access the variable would be creating a function inside your class like this:

访问变量的正确方法是在类中创建一个函数,如下所示:

public function getFollowee() {
  return $this -> followee;
}

And the way to access it:

以及访问它的方式:

function activity() {
  $query_str = 'SELECT DISTINCT name FROM place WHERE userid IN ( ' . $stdClass -> getFollowee() . ')';
  $query = $this -> db -> query( $query_str );
}

#1


2  

Try $your_array[0]->followee.

#2


0  

Well, you have an object in you variable and you don't want to convert that.

好吧,你有一个变量对象,你不想转换它。

The right way, to access the variable would be creating a function inside your class like this:

访问变量的正确方法是在类中创建一个函数,如下所示:

public function getFollowee() {
  return $this -> followee;
}

And the way to access it:

以及访问它的方式:

function activity() {
  $query_str = 'SELECT DISTINCT name FROM place WHERE userid IN ( ' . $stdClass -> getFollowee() . ')';
  $query = $this -> db -> query( $query_str );
}