title: PHP, PDO, MySQL, Notice: Trying to get property of non-object
body: <p>I'm still trying to wrap my head around php, so sorry if this is a simple mistake (I've been searching for quite a while and can only manage to get different errors such as "undefined index")</p>
  
  <p>What I'm attempting to do is have a function that will get data from a table (it doesn't contain a great deal at the moment, but will eventually contain everything for each main webpage). It should take an id then give the correct template and content for that page (just hard coding the $pageName in for now until I get it working)</p>
  
  <p>in my Page class I have this</p>
  
  <pre><code>class Page{
  
  public $pageid = null;
  public $pagetemplate = null;
  public $pagetitle = null;
  public $pagebody = null;
  
  public function __construct($data=array()){
      if(isset($data['page_id'])) $this-&gt;pageid = (int) $data['page_id'];
      if(isset($data['page_template'])) $this-&gt;pagetemplate = (string) $data['page_template'];
      if(isset($data['page_title'])) $this-&gt;pagetitle = (string) $data['page_title'];
      if(isset($data['page_body'])) $this-&gt;pagebody = (string) $data['page_body'];
  }
  
  public static function getPageData($pageName){
      $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
      $sql = "SELECT  page_template, page_title, page_body FROM pages WHERE page_id = :page_id LIMIT 1";
      $stmt = $conn-&gt;prepare($sql);
      $stmt-&gt;bindParam(':page_id', $pageName);
      $stmt-&gt;execute();
      $list = array();
      while($row = $stmt-&gt;fetch()){
          //$pageslug = $row['page_slug'];
          $page = new Page($row);
          $list[] = $page;
  
      }
  
      $conn = null;
      return(array("results" =&gt; $list));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>then in my index I have </p>
  
  <pre><code>function homepage(){
      $results = array();
      $data = Page::getPageData(1);
      $results['pages'] = $data['results'];
      $template = $results['pages']-&gt;pagetemplate;
      require($template);
  }
  </code></pre>
  
  <p>The homepage function is called with a switch case but I'll change that later on so that the navbar changes the value given to getPageData and that should be how different pages are loaded. Anyway, I'm getting the following errors:</p>
  
  <p>Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 48</p>
  
  <p>Warning: require(): Filename cannot be empty in C:\xampp\htdocs\index.php on line 49</p>
  
  <p>Fatal error: require(): Failed opening required '' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\index.php on line 49</p>