Trying to get property of non-object (2)
I keep getting this errror:
Trying to get property of non-object.
The problem is with a SQL date field called 'sterfdatum' when this field in my DB is filled in with for example '1988-12-01 15:13:14'. Then there is no problem with my code. But when field is like this '0000-00-00 00:00:00', and I try to get this out from my DB the error is showed.
this is my model:
<?php
public function get_artist($last_name = NULL , $front_name = NULL, $language = 'nl')
{
// infoNL as language so language can be used in the View file
if($language == 'nl')
{
$this->db->select('naam, voornaam, geboortedatum, geboorteplaats, foto, infoNL as language, sterfdatum')
->from('kunstenaar')
->where('naam', $last_name)
->where('voornaam', $front_name)
->limit(1);
return $this->db->get()->row();
}
elseif($language == 'en')
{
$this->db->select('naam, voornaam, geboortedatum, geboorteplaats, foto, infoEN as language, sterfdatum')
->from('kunstenaar')
->where('naam', $last_name)
->where('voornaam', $front_name)
->limit(1);
return $this->db->get()->row();
}
}
In my view I have this
if(开发者_如何学Go$artist->sterfdatum != '0000-00-00 00:00:00')
{
$birthYear = date('Y', strtotime($artist->geboortedatum));
$dieYear = date('Y', strtotime($artist->sterfdatum));
}
$artist->sterfdatum != '0000-00-00 00:00:00' And here is the problem.
if(isset($artist->sterfdatum) && $artist->sterfdatum != '0000-00-00 00:00:00') {
$birthYear = date('Y', strtotime($artist->geboortedatum));
$dieYear = date('Y', strtotime($artist->sterfdatum));
}
should work
If I understand correctly, it seems that you're assigning a variable artist that is not an object to your view.
This error probably occurs when no match for your query are found in your database.
Assuming that your method get_artist() returns a NULL, you should try something like this :
if(isset($artist))
{
if($artist->sterfdatum != '0000-00-00 00:00:00')
{
$birthYear = date('Y', strtotime($artist->geboortedatum));
$dieYear = date('Y', strtotime($artist->sterfdatum));
}
}
However you should look what's the value in $artist when the error occurs to make the correct test for it.
Hope it helps you.
精彩评论