试图从数据库中读取数据,遇到PHP错误

时间:2016-03-04 18:25:57

标签: php

我正在尝试使用PHP OOP从我的数据库中读出一些文章。但现在我有这两个错误,我真的找不到任何解决方案。

  

注意:未定义的变量:/ Applications / MAMP / htdocs / web中的id   第57行的2.0 / projektet / class / pagesClass.php

  

警告:为foreach()提供的参数无效   第31行的/ Applications / MAMP / htdocs / web 2.0 / projektet / index.php

我的代码

$page = new CMS();
$gp = $page->getPage();


foreach ($gp as $sp) {
  //var_dump($sp);

  echo "<div class='pub'>";
  echo "<h4 class='pub-headline'>" . $sp['title'] . "</h4>"; 
  echo "<article class='pub_art'>" . $sp['content'] . "</article>";  
  echo "<p class='pub_created'>" . $sp['created'] . "</p>"; 
  echo "<p class='pub_created_by'>". $sp['writer'] ."</p>";
  echo "<button class='show'>Show</button>"; 
  echo "<button class='noshow'>Hide</button>";

  echo "<button class='btn-like'>Like</button>";

  echo "</div>";

}


  // >>>>>>>>>>>>>>>>>>>>>>>> Function for reading out page 
  public function getPage() {

    //$id = intval($id);

    $sql = "SELECT * FROM pages
     WHERE id = '$id' ";
    $result = mysqli_query($this->db, $sql) or die('Fel vid SQL fråga GET PAGE');

    if(mysqli_num_rows($result)) {
        return mysqli_fetch_all($result, MYSQLI_ASSOC);
    } else {
        return false;
    } 


}

0 个答案:

没有答案