我试图做的是有一个函数可以从表中获取数据(它目前不包含大量数据,但最终将包含每个主网页的所有内容).它应该需要一个 id 然后为该页面提供正确的模板和内容(现在只是硬编码 $pageName 直到我让它工作)在我的页面类中,我有这个class Page{公共 $pageid = null;公共 $pagetemplate = null;公共 $pagetitle = null;公共 $pagebody = null;公共函数 __construct($data=array()){if(isset($data['page_id'])) $this->pageid = (int) $data['page_id'];if(isset($data['page_template'])) $this->pagetemplate = (string) $data['page_template'];if(isset($data['page_title'])) $this->pagetitle = (string) $data['page_title'];if(isset($data['page_body'])) $this->pagebody = (string) $data['page_body'];}公共静态函数 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->prepare($sql);$stmt->bindParam(':page_id', $pageName);$stmt->execute();$list = 数组();while($row = $stmt->fetch()){//$pageslug = $row['page_slug'];$page = new Page($row);$list[] = $page;}$conn = 空;return(array("results" => $list));}}然后在我的索引中我有函数主页(){$results = array();$data = Page::getPageData(1);$results['pages'] = $data['results'];$template = $results['pages']->pagetemplate;要求($模板);}主页函数是用 switch case 调用的,但我稍后会更改它,以便导航栏更改给 getPageData 的值,这应该是加载不同页面的方式.无论如何,我收到以下错误:注意:第 48 行试图获取 C:\xampp\htdocs\index.php 中非对象的属性警告:require():第 49 行 C:\xampp\htdocs\index.php 中的文件名不能为空致命错误:require(): Failed opening required '' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\index.php 第 49 行 解决方案 您的 getPageData() 方法在results"索引处返回一个数字索引数组,而不是一个对象.这意味着 $results['pages'] 包含一个数字索引数组,而不是一个对象.var_dump() 并检查它.您可能需要执行以下操作:$template = $results['pages'][0]->pagetemplate;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")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)in my Page class I have thisclass Page{public $pageid = null;public $pagetemplate = null;public $pagetitle = null;public $pagebody = null;public function __construct($data=array()){ if(isset($data['page_id'])) $this->pageid = (int) $data['page_id']; if(isset($data['page_template'])) $this->pagetemplate = (string) $data['page_template']; if(isset($data['page_title'])) $this->pagetitle = (string) $data['page_title']; if(isset($data['page_body'])) $this->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->prepare($sql); $stmt->bindParam(':page_id', $pageName); $stmt->execute(); $list = array(); while($row = $stmt->fetch()){ //$pageslug = $row['page_slug']; $page = new Page($row); $list[] = $page; } $conn = null; return(array("results" => $list));}}then in my index I have function homepage(){ $results = array(); $data = Page::getPageData(1); $results['pages'] = $data['results']; $template = $results['pages']->pagetemplate; require($template);}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:Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 48Warning: require(): Filename cannot be empty in C:\xampp\htdocs\index.php on line 49Fatal error: require(): Failed opening required '' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\index.php on line 49 解决方案 Your getPageData() method returns a numerically indexed array at the 'results' index, not an object. This means $results['pages'] contains a numerically indexed array, not an object. var_dump() it and check it out.You likely need to do something like:$template = $results['pages'][0]->pagetemplate; 这篇关于PHP、PDO、MySQL,注意:尝试获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 07:56