2013-03-28 30 views
0

使用PHP和MySQL我試圖建立自己的CMS,但下面的教程,我得到這個代碼

在cms_class.php時

<?php 
class modernCMS{ //starts class 
    var $host; 
var $username; 
var $password; 
var $db; 
function connect(){ 
      $con=  mysql_connect( $this -> host, $this->username, $this->password); 
    mysql_select_db($this->db, $con) or die (mysql_error()) ; 
}// ends function 

function get_content(){ 
    $query= "SELECT * 
FROM cms_content ORDER BY id DESC"; 
    $result= mysql_query($$query); 

    while($row= mysql_fetch_assoc($res)){ 
     echo '<h1>' . $row['title'] . '</h1>'; 
     echo '<p>' . $row['body'] . '</p>'; 
    } 
} 

} //Ends class 

?> 

然後我的索引頁上我有(PHP第一)

<?php 

include '_class/cms_class.php'; 

$obj= new modernCMS(); 

//set up connection variables 
$obj->host='localhost'; 
$obj->username='root'; 
$obj->password=''; 
$obj->db='modernCMS'; 

//Connection to the DB 

$obj->connect(); 


?> 

那麼php來得到我的cms_content表中的內容是

<?=$obj-> get_content()?> 

,當我的本地主機服務器我得到這些錯誤上運行....

未定義的變量cms_class.php線18 mysql_fetch_assoc()

modernCMS-> get_content我的索引。 php line 34

爲什麼不能正常工作?

回答

1

在你get_content函數創建變量$result但隨後傳遞變量$resmysql_fetch_assoc不存在。您在mysql_query的電話$query上也加了$$,我也將其刪除。

function get_content(){ 
    $query = "SELECT * FROM cms_content ORDER BY id DESC"; 
    $result = mysql_query($query); 

    while($row= mysql_fetch_assoc($result)){ 
     echo '<h1>' . $row['title'] . '</h1>'; 
     echo '<p>' . $row['body'] . '</p>'; 
    } 
}