【问题标题】:Object of class stdClass could not be converted to string (Error Number: 1064)类 stdClass 的对象无法转换为字符串(错误号:1064)
【发布时间】:2019-04-09 20:58:51
【问题描述】:

我不断收到此错误(检查问题底部的确切错误)我一次又一次地检查,但找不到问题所在。似乎没事。在 insert_batch 上它工作正常,但在 update_batch 上我收到此错误。这是我当前的代码

Codeigniter 模型: 这是正确获取数据的模型

    function get_seeds($tournament_id) {
        $this->db->where('tournament_id', $tournament_id);
        $result = $this->db->get('tournament_seed');
        return $result->result();
    }

Codeigniter 控制器: 这是我的控制器,我在其中检查是否有种子行。如果是,则更新,否则插入。

    $check_seeds = (object)$this->tournament_model->get_seeds($tournament_id);
    if ($_POST) {
        foreach ($check_seeds as $key => $value) {
            if(!empty($key) && !empty($value)) {
                $seeded[] = array(
                    'id' => $value->id,
                    'tournament_id' => $tournament_id,
                    'stage_id' => $stage_id,
                    'seed_id' => $value,
                    'team_name' => $key,
                );
                $this->db->update_batch('tournament_seed', $seeded, 'id');
                redirect('organizer/tournaments);
            } else {
                //something
            }
        }
    }

Print_r($check_seeds)

    stdClass Object
    (
        [0] => stdClass Object
        (
            [id] => 3
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 1
            [team_name] => -V-ATTAX
        )

        [1] => stdClass Object
        (
            [id] => 4
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 2
            [team_name] => NIP
        )

        [2] => stdClass Object
        (
            [id] => 5
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 3
            [team_name] => fnatic
        )
    )

错误

    A PHP Error was encountered
    Severity: 4096

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

    Filename: database/DB_query_builder.php

    Line Number: 1970

    Backtrace:

    File: E:\xampp\htdocs\hltv\application\controllers\organizer\Tournaments.php
    Line: 1065
    Function: update_batch

    File: E:\xampp\htdocs\hltv\index.php
    Line: 315
    Function: require_once


    Error Number: 1064

    You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSE `seed_id` END, `team_name` = CASE WHEN `id` = '4' THEN '1' ELSE `team_name' at line 7

    UPDATE `tournament_seed` SET `tournament_id` = CASE WHEN `id` = '4' THEN '713161746' ELSE `tournament_id` END, `stage_id` = CASE WHEN `id` = '4' THEN '3' ELSE `stage_id` END, `seed_id` = CASE WHEN `id` = '4' THEN ELSE `seed_id` END, `team_name` = CASE WHEN `id` = '4' THEN '1' ELSE `team_name` END WHERE `id` IN('4')

    Filename: E:/xampp/htdocs/hltv/system/database/DB_driver.php

    Line Number: 691

【问题讨论】:

    标签: codeigniter codeigniter-3


    【解决方案1】:

    您正在尝试对刚刚从数据库表 tournament_seed 查询的相同数据使用 update_batch(),但您没有使用新的 $_POST 要更新的数据。

    您还在循环中使用 update_batch(),这是错误的。循环只是创建要更新的数组,然后在循环之外 update_batch()

    使用以下内容创建一个数组,稍后可以将其用于update_batch(),类似于:

    $seeded=array();
    $sd=array();        
    foreach($_POST['your_newly_posted_seeds'] as $key=>$value){
        $sd['id']=$value->id;
        $sd['tournament_id']=$tournament_id;
        //...
        array_push($seeded, $sd);
    }
    
    $this->db->update_batch('tournament_seed', $seeded, 'id');
    

    update_batch() from the docs,向下滚动或搜索:$this->db->update_batch() 因为它没有书签。

    【讨论】:

    • 哇!我到底犯了什么愚蠢的错误。感谢您的帮助。
    猜你喜欢
    • 2013-07-03
    • 2014-11-21
    • 2017-09-21
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多