Page 1 of 1

casting an object to a string

Posted: Mon Feb 09, 2009 6:10 pm
by neridaj
Hello,

I'm working on a script that creates an invoice number based on the current date and the record count returned from the db for a particular year. I'm trying to get the invoice number to be in the form of YYYYMMDDNN where NN represents the count returned from the db. However, I can't simply add the two together because that results in DD being incremented, and if I try to cast the count I get a "Catchable fatal error: Object of class mysqli_result could not be converted to string". Any suggestions?

Code: Select all

 
function get_invoice_number()
{
    $year = date("Y");
    $today = date("Ymd");
    $conn = db_connect();
    $count = $conn->query("select count(inv_number) from invoice where inv_number like '$year%'");
    if(!$count)
        return false;
    else
        $invnum = $today.(string)$count;
        return $invnum;
}
 
Thanks,

J

Re: casting an object to a string

Posted: Mon Feb 09, 2009 6:20 pm
by neridaj
I got it figured out.

Re: casting an object to a string

Posted: Mon Feb 09, 2009 6:24 pm
by John Cartwright
your best best is to implement the __toString() method to handle the conversion.

P.S. when you solve a problem, it is common curtosy to post how you came to a solution (for future readers).