php 可捕获的致命错误:当尝试插入数据库时​​,类 stdClass 的对象无法转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9094276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:08:36  来源:igfitidea点击:

Catchable fatal error: Object of class stdClass could not be converted to string in..when tried to insert into database

phpmysqlfatal-error

提问by AssamGuy

I have found similar queries like mine in stackoverflow,but found no solutions . So I am asking it again. I have the following insertquery :

我在 stackoverflow 中发现了类似我的查询,但没有找到解决方案。所以我又来问了。我有以下insert查询:

$purchase_date = date("Y-m-d");

    $init = substr($info[fname], 0, 2);
    $odr = rand(0,255);

    $invoice_number = $this->get_invoice_number();

    //$invoice_number = $invoice_number+1;
    //$invoice_number = 400 + rand(0,100);
    $order_number = $init.'-'.$odr;


    $session_id = session_id();

    $sql = "
        INSERT INTO
            tbl_checkout
        SET
            fname = '$info[fname]',
            lname = '$info[lname]',
            email = '$info[email]',
            phone = '$info[phone]',
            address = '$info[address]',
            pin = '$info[pin]',
            session_id = '$session_id',
            purchase_date = '$purchase_date',
            invoice_number = '$invoice_number',
            order_number = '$order_number'             <----This is line no 1038
    ";
    $this->db->insertQuery($sql);

But when I tried to execute it, it shows error like Catchable fatal error: Object of class stdClass could not be converted to string in c:\.....on line 1038

但是当我尝试执行它时,它显示如下错误 Catchable fatal error: Object of class stdClass could not be converted to string in c:\.....on line 1038

I am lost because I can't even understand what the error means ! Please help.

我迷路了,因为我什至无法理解错误的含义!请帮忙。

回答by konsolenfreddy

$this->get_invoice_number()is probably returning an object.

$this->get_invoice_number()可能正在返回一个对象。

you can cast it to a string:

您可以将其转换为字符串:

$invoice_number = (string) $this->get_invoice_number();

回答by ajreal

There is nothing wrong in line 1038,
most likely is line 1037 having an error
(valuable casting issue)

第 1038 行没有任何问题,
很可能是第 1037 行有错误
(宝贵的铸造问题)

$invoice_number = $this->get_invoice_number();

You should do a

你应该做一个

var_dump( $this->get_invoice_number() );

Then likely you just need to refer to the object property like

那么可能你只需要引用像这样的对象属性

$invoice_number->SOME_PROPERTY;