dtc99987 2015-08-02 16:31
浏览 159
已采纳

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

I am using this code to insert

$last_id = "SELECT max(id) as id from clients ";
    $client_id = $this->db->query($last_id)->row();
    $client_last = $client_id->id;
    $data = array();    
for($i=0; $i<count($res); $i++)
{
$data['user_id'] = $res[$i];
$data['client_id']=$client_last;
$this->db->insert("notifications", $data);
}

Array $res returns values user_ids like below:

 Array
(
[0] => stdClass Object
    (
        [id] => 5
    )

[1] => stdClass Object
    (
        [id] => 6
    )

[2] => stdClass Object
    (
        [id] => 7
    )

)

And variable $client_last returns single value like 5 or 90 something like this.

In this scenario I want to insert three rows in table notifications inserting the value of $res array and same value of $client_last against each value of $res array. But in this case I am getting data base error saying:

Message: Object of class stdClass could not be converted to string

and when I print last query with $this->db->last_qery(); It shows something like this

INSERT INTO `notifications` (`user_id`, `client_id`) VALUES (, '90')

with saying sql error.

  • 写回答

1条回答 默认 最新

  • dousi6405 2015-08-02 16:41
    关注

    I think that your problem is in this line:

    $data['user_id'] = $res[$i];
    

    You should change it to:

      $data['user_id'] = $res[$i]->id;
    

    Because the id's values are in the object stored in the array $res

    The code:

    for($i=0; $i<count($res); $i++)
    {
      $data['user_id'] = $res[$i]->id;
      $data['client_id'] = $client_last;
      $this->db->insert("notifications", $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里