开发者

php class not responding

I have a problem with the following piece of PHP script:

<?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!
      */
}
    }

    ?>

For those, who do not want to read source: I create an abstract class, that creates database connection, then other classes, like login extends it, using the parents database connection.

The problem is, whenever I create an instance of a class, like:

$map = new map();

Then try to do something with the instance:

$map->setMarkers("");

Nothing happens. Literally nothing. No error message, and no output.

All help would be highly appreciated!


You have some incorrect function definitions:

protected function __construct__() {

should read

protected function __construct() {

The same is true for the __destruct__() function - this should read __destruct()

However you should certainly be receiving some error reporting - try setting

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

at the top of the script.


First, check the return values of each of the mysql function calls. They all return valuable information. For example, mysql_connect():

Returns a MySQL link identifier on success or FALSE on failure.

Also, in your constructor, you're closing the database connection before selecting the DB, which doesn't really make sense. Remove this line:

mysql_close($this->con);

and see what happens. Actually, on second read, remove that call from all functions except the destructor. It might not even be necessary there, since DB connections are closed on script termination, I believe.


In your constructor :

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);
}

remove close connection, you closing connector on destruct ... it should be like following :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜