conditional code causes undefined property error

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
kevinwinters
Forum Newbie
Posts: 20
Joined: Wed Jan 06, 2010 8:31 pm

conditional code causes undefined property error

Post by kevinwinters »

Hi,
I am trying to add an if statement to my select statement in MYSQL but I keep getting a "Notice: Undefined property: stdClass::$date_sold " error when I try.

Here is the code snippet that works fine:

Code: Select all

 
$sql = "
    SELECT 
        i.id,
        i.name,
        i.featured_item,
        i.sales_status,
        c.name room_type,
        im.name image,
        i.date_added,
        i.date_last_modified,
        i.date_sold
    FROM 
        inventory i
etc.....
 
 
But when I try to do this

Code: Select all

 
$sql = "
    SELECT 
        i.id,
        i.name,
        i.featured_item,
        i.sales_status,
        c.name room_type,
        im.name image,
        i.date_added,
        i.date_last_modified,
        CASE i.date_sold
              WHEN '0000-00-00 00:00:00' THEN 'Not Sold'
        END
    FROM 
        inventory i
 
I get the error

I am just trying to display something else on my PHP page if the datetime field is '0000-00-00 00:00:00'

Could anyone help with this?


Thanks in advance
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: conditional code causes undefined property error

Post by pbs »

You can try IF statement like this

Code: Select all

 
$sql = "SELECT i.id, i.name, i.featured_item, i.sales_status, c.name room_type, im.name image, i.date_added, i.date_last_modified, IF(i.date_sold = '0000-00-00 00:00:00', 'Not Sold', '') FROM inventory i
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: conditional code causes undefined property error

Post by requinix »

You probably need an alias. Using pbs's modification,

Code: Select all

SELECT i.id, i.name, i.featured_item, i.sales_status, c.name room_type, im.name image, i.date_added, i.date_last_modified, IF(i.date_sold = '0000-00-00 00:00:00', 'Not Sold', '') AS date_sold FROM inventory i
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: conditional code causes undefined property error

Post by Eran »

The error notice you are seeing has nothing to do with the SQL statement.
Notice: Undefined property: stdClass::$date_sold
You are trying to access a property called date_sold on an object somewhere in your code, and that property is not declared.

Regrading your case statement, you are missing an ELSE condition - if no condition matches the case, MySQL will return a "Case not found" error.
[sql]SELECT         i.id,         i.name,         i.featured_item,         i.sales_status,         c.name room_type,         im.name image,         i.date_added,         i.date_last_modified,         (CASE i.date_sold               WHEN '0000-00-00 00:00:00' THEN 'Not Sold'               ELSE i.date_sold         END) AS date_sold     FROM         inventory i[/sql]
http://dev.mysql.com/doc/refman/5.0/en/ ... ement.html
kevinwinters
Forum Newbie
Posts: 20
Joined: Wed Jan 06, 2010 8:31 pm

Re: conditional code causes undefined property error

Post by kevinwinters »

Thank You,

That worked without an error
Post Reply