我对以下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!
*/
}
}
?>
对于那些不想阅读源代码的人:我创建了一个创建数据库连接的抽象类,然后使用parent数据库连接等其他类(如login扩展它)。
问题是,每当我创建一个类的实例时,如:
$map = new map();
然后尝试对实例做一些事情:
$map->setMarkers("");
什么都没发生。没什么。没有错误消息,也没有输出。
所有帮助都将受到高度赞赏!
答案 0 :(得分:2)
您有一些不正确的功能定义:
protected function __construct__() {
应该阅读
protected function __construct() {
__destruct__()
函数也是如此 - 这应该是__destruct()
但是你肯定会收到一些错误报告 - 尝试设置
error_reporting(E_ALL);
ini_set('display_errors', 1);
位于脚本的顶部。
答案 1 :(得分:0)
首先,检查每个mysql函数调用的返回值。他们都返回有价值的信息。例如,mysql_connect():
成功时返回MySQL链接标识符,失败时返回FALSE。
此外,在构造函数中,您在选择数据库之前关闭数据库连接,这实际上没有意义。删除这一行:
mysql_close($this->con);
看看会发生什么。实际上,在第二次读取时,从析构函数以外的所有函数中删除该调用。在那里甚至可能没有必要,因为我相信,在脚本终止时关闭数据库连接。
答案 2 :(得分: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);
}
删除关闭连接,关闭destruct上的连接器......它应该如下:
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 ...