2010-11-13 77 views
1

我有下面這段PHP腳本的問題:PHP類沒有響應

<?php 
abstract class root { 
protected $con; 
protected $sql; 
protected $query; 
protected $row; 

protected function __construct__() { 
    require_once("db.settings.php"); 

    $this->con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error($this->con)); 
    mysql_query("SET NAMES UTF8", $this->con) or die(mysql_error($this->con)); 
    mysql_close($this->con); 
    mysql_select_db(DB_NAME, $this->con); 
} 

protected function __destruct__() { 
    mysql_close($this->con); 
    unset($this->con); 
} 

    } 

    class users extends root { 
private $h; 

public function __construct__() { 
    parent::__construct__(); 
} 

public function __destruct__() { 
    parent::__destruct__(); 
} 

public function register($uname, $pass, $mail, $name) { 
    $this->hash = sha1($uname . md5($pass) . $name); 

    $this->sql = "INSERT INTO users (user, pass, mail, name, hash) VALUES ($uname, $pass, $mail, $name, $this->hash)"; 
    mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 
    mysql_close($this->con); 

    return true; 
} 

public function login($uname, $pass) { 
    $this->sql = "SELECT * FROM users WHERE user = $uname AND pass = " . md5($pass); 
    mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 

    if(mysql_num_rows !== 1){ 
     mysql_close($this->con); 

     return false; 
    } else { 
     setcookie("who", $uname, time() + 3600); 
     $this->h = md5($pass); 
     setcookie("token", $this->h, time() + 3600); 
     $this->h = sha1($uname . md5($pass)); 
     setcookie("hash", $uname, time() + 3600); 

     return true; 
    } 
} 

public function logout() { 
    if(isset($_COOKIE['who']) && isset($_COOKIE['token']) && isset($_COOKIE['hash']) && $_COOKIE['hash'] == sha1($_COOKIE['who'] . $_COOKIE['token'])) { 
     setcookie("who", $uname, time() - 1); 
     setcookie("token", $uname, time() - 1); 
     setcookie("hash", $uname, time() - 1); 

     return true; 
    } else { 
     setcookie("who", $uname, time() - 1); 
     setcookie("token", $uname, time() - 1); 
     setcookie("hash", $uname, time() - 1); 

     return false; 
    } 
} 

public function check() { 
    if(isset($_COOKIE['who']) && isset($_COOKIE['token']) && isset($_COOKIE['hash']) && $_COOKIE['hash'] == sha1($_COOKIE['who'] . $_COOKIE['token'])) { 
     return true; 

    } else { 
     return false; 
    } 
} 
    } 

    class iPhone extends root { 
private $hash; 
public $content = array(); 
private $j = 0; 

public function __construct__() { 
    parent::__construct__(); 
} 

public function __destruct__() { 
    parent::__destruct__(); 
} 

public function registerIphone() { 
    # implented later 
    $this->hash = md5(time); 

    $this->sql = "UPDATE users SET phoneid = " . $this->hash . " WHERE user = " . $_COOKIE['who']; 
    mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 
    setcookie("phoneid", $this->hash, 1314000); 

    return true; 
} 
public function checkIphone() { 
    # implented later 

    $this->sql = "SELECT * FROM users WHERE phoneid = " . $_COOKIE['phoneid'] . " LIMIT 1"; 
    if(mysql_query($this->sql, $this->con)) { 
     return true; 
    } else { 
     return false; 
    } 

} 
public function deleteIphone($who) { 
    # implented later 

    $this->sql = "UPDATE users SET phoneid = '' WHERE user = $who"; 
    if(mysql_query($this->sql, $this->con)) { 
     return true; 
    } else { 
     return false; 
    } 
} 
public function showContent($what) { 
    $this->sql = "SELECT * FROM content WHERE articleName = " . $what . " AND media = Handheld"; 
    $this->query = mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 
    while($this->row = mysql_fetch_array($this->query)) { 
     $content[$j]['id']   = $row['id']; 
     $content[$j]['date']  = $row['date']; 
     $content[$j]['articleName'] = $row['articleName']; 
     $content[$j]['content']  = $row['content']; 

     $this->j++; 
    } 
    mysql_close($this->con); 
    return $this->content; 
} 
    } 

    class contentManager extends root { 
public $content = array(); 
private $j = 0; 

public function __construct__() { 
    parent::__construct__(); 
} 

public function __destruct__() { 
    parent::__destruct__(); 
} 

public function showContent($what) { 
    $this->sql = "SELECT * FROM content WHERE articleName = $what AND media = Browser"; 
    $this->query = mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 
    while($this->row = mysql_fetch_array($this->query)) { 
     $content[$j]['id']   = $row['id']; 
     $content[$j]['date']  = $row['date']; 
     $content[$j]['articleName'] = $row['articleName']; 
     $content[$j]['content']  = $row['content']; 

     $this->j++; 
    } 
    mysql_close($this->con); 
    return $this->content; 
} 

public function editContent($id, $articlename, $content) { 
    $this->sql = "UPDATE content SET articleName = $articlename, content = $content WHERE id = $id"; 
    mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 

    return true; 
} 

public function deleteContent($id) { 
    $this->sql = "DELETE FROM content WHERE id = $id"; 
    mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 

    return true; 
} 
    } 

    class map extends root { 
private $j = 0; 
private $key = "my google maps api key"; 
private $address; 
private $helper; 
private $page; 
private $xml; 
private $longitude; 
private $latitude; 
private $altitude; 

public function __construct__() { 
    parent::__construct__(); 
} 

public function __destruct__() { 
    parent::__destruct__(); 
} 

public function getMarkers() { 
    $this->sql = "SELECT * FROM markers"; 
    $this->query = mysql_query($this->sql, $this->con) or die(mysql_error($this->con)); 
    while($this->row = mysql_fetch_array($this->query)) { 
     if($this->j < (mysql_num_rows($this->query) -1)){ 
      print("['" . $this->row['address'] . "', " . $this->row['lat'] . ", " . $this->row['lng'] . ", 1],"); 
      $this->j++; 
     } else { 
      print("['" . $this->row['address'] . "', " . $this->row['lat'] . ", " . $this->row['lng'] . ", 1]"); 
      $this->j++; 

     } 
    } 

} 

public function setMarkers($addr) { 
    $this->helper = str_replace(" ", "+", $addr); 
    $this->helper = explode(",", $this->helper); 
    $this->address = "http://maps.google.com/maps/geo?q=$this->helper[0],+$this->helper[1],+$this->helper[2]&output=xml&key=$this->key"; 
    $this->page = file_get_contents($this->address); 
    $this->xml = new SimpleXMLElement($this->page); 
    list($this->longitude, $this->latitude, $this->altitude) = explode(",", $xml->Response->Placemark->Point->coordinates); 

    echo $this->longitude . " :: " . $this->latitude; 



} 

public function deleteMarkers($id) { 
    /* 
    *** FUNCTION NOT YET IMPLENTED! 
     */ 
} 
    } 

    ?> 

對於那些,誰不想讀源:我創建了一個抽象類,創建數據庫連接,那麼其他類,如登錄擴展它,使用父母數據庫連接。

的問題是,每當我創建一個類的實例,如:

$map = new map(); 

然後嘗試做一些與實例:

$map->setMarkers(""); 

什麼也沒有發生。毫無意義。沒有錯誤信息,也沒有輸出。

所有幫助將不勝感激!

+1

您是否嘗試過調試?例如增加'print'語句來找出它卡住的地方? – 2010-11-13 22:45:20

+0

是的,試圖從根和子類構造函數&&析構函數打印字符串。 – 2010-11-13 22:50:25

回答

2

你有一些不正確的函數定義:

protected function __construct__() {

應該讀

protected function __construct() {

同樣是爲__destruct__()功能如此 - 這應該閱讀__destruct()

但是你應該肯定會收到一些錯誤報告 - 嘗試設置

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

位於腳本的頂部。

+0

我以爲我知道這個......的語法;-) – 2010-11-13 22:53:53

0

首先,檢查每個mysql函數調用的返回值。他們都返回有價值的信息。例如,mysql_connect():

返回成功時的MySQL鏈接標識或失敗時返回FALSE。

此外,在您的構造函數中,在選擇數據庫之前關閉數據庫連接,這並不合理。刪除此行:

mysql_close($this->con); 

看看會發生什麼。實際上,在第二次讀取時,除了析構函數之外,從所有函數中移除該調用。這可能甚至沒有必要,因爲我相信DB連接在腳本終止時是關閉的。

+0

完美有道理,謝謝! – 2010-11-13 22:54:34

0

在你的構造:

protected function __construct() { 
    require_once("db.settings.php"); 

    $this->con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error($this->con)); 
    mysql_query("SET NAMES UTF8", $this->con) or die(mysql_error($this->con)); 
    mysql_close($this->con); 
    mysql_select_db(DB_NAME, $this->con); 
} 

除去緊密的聯繫,您收盤毀滅連接器...它應該像下面:

protected function __construct() { 
     require_once("db.settings.php"); 

     $this->con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error($this->con)); 
     mysql_query("SET NAMES UTF8", $this->con) or die(mysql_error($this->con)); 
     mysql_select_db(DB_NAME, $this->con); 
    } 

Change construct and destruct to right ones : __destruct__() to __destruct() same with construct ... 
+0

您可能也想更改方法名稱。 – 2010-11-13 22:54:51

+0

我使用抽象,因爲我不希望根類自身實例化,只是當某個子類正在使用它的數據庫連接時。 – 2010-11-13 22:55:43

+0

sory做了一些編輯..它是從你寫的內容複製..調用構造函數如下__construct($ methods) – Conex 2010-11-13 22:56:38