casting an object to a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

casting an object to a string

Post 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
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Re: casting an object to a string

Post by neridaj »

I got it figured out.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: casting an object to a string

Post 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).
Post Reply