php - Retrieve Results from DB with Foreach -
i thought easier thing do, i'm trying better coding practices , keep hearing while not way loop results, i'm trying switch , thought had down, no dice. original code have been
public function getsitename() { $query = <<<sql select site_name site_details sql; $resource = $this->db->prepare( $query ); $resource->execute(); while($row = $resource->fetch(pdo::fetch_assoc)){ echo $row['site_name']; } }
so i've tried changing foreach loop , i've gotten million different ways using mysql extensions. (i'm positive 99% of people it's better use while loop go mysql , use deprecated function) instead i'm trying isn't working
public function getsitename(){ $query = <<<sql select site_name site_details sql; $resource = $this->db->prepare( $query ); $resource->execute(); $result = $resource->fetch(pdo::fetch_assoc); foreach($result $detail){ echo $detail->site_name; } }
i'm not sure i'm doing wrong on here, first 1 works fine, second 1 gives me:
notice: trying property of non-object in c:\xampp\htdocs\functions.d\db.class.php on line 37
however if try running print_r($result);
get
array ( [site_name] => doxramos )
i think that's many details can put, feel i'm doing correct, notice doesn't lie. i'm not sure how can on line 37 trying non object when i'm pulling array.
you're trying echo
object whilst $detail
array. change below
echo $detail->site_name;
to:
echo $detail['site_name'];
Comments
Post a Comment