用户在线脚本出现未知错误

时间:2016-03-10 07:52:12

标签: php mysqli

我正在尝试使这个脚本在mysqli中运行,脚本才能在mysql中运行,所以我试图'重制它'但它返回错误而我不知道该怎么办? :

未定义的变量:conn

我做错了什么?

<?
     $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost 
        $user = "my_user"; // your MySQL username 
        $pass = "mypassword"; // your MySQL password 
        $db = "my_db"; // the database to which you're trying to connect to


        //start database
         $conn = mysqli_connect($host,$user,$pass,$db);


        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }





        class usersOnline {


            var $timeout = 600;
            var $count = 0;
            var $error;
            var $i = 0;

            function usersOnline () {
                $this->timestamp = time();
                $this->ip = $this->ipCheck();
                $this->new_user();
                $this->delete_user();
                $this->count_users();
            }

            function ipCheck() {

                if (getenv('HTTP_CLIENT_IP')) {
                    $ip = getenv('HTTP_CLIENT_IP');
                }
                elseif (getenv('HTTP_X_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_X_FORWARDED_FOR');
                }
                elseif (getenv('HTTP_X_FORWARDED')) {
                    $ip = getenv('HTTP_X_FORWARDED');
                }
                elseif (getenv('HTTP_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_FORWARDED_FOR');
                }
                elseif (getenv('HTTP_FORWARDED')) {
                    $ip = getenv('HTTP_FORWARDED');
                }
                else {
                    $ip = $_SERVER['REMOTE_ADDR'];
                }
                return $ip;
            }

            function new_user() {
                $insert = mysqli_query ($conn,"INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
                if (!$insert) {
                    $this->error[$this->i] = "Unable to record new visitor\r\n";            
                    $this->i ++;
                }
            }

            function delete_user() {
                $delete = mysqli_query ($conn,"DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
                if (!$delete) {
                    $this->error[$this->i] = "Unable to delete visitors";
                    $this->i ++;
                }
            }

            function count_users() {
                if (count($this->error) == 0) {
                    $count = mysqli_num_rows ( mysqli_query($conn,"SELECT DISTINCT ip FROM useronline"));
                    return $count;
                }
            }

        }

        ?>

2 个答案:

答案 0 :(得分:0)

尝试以下

<?

class usersOnline {


    var $timeout = 600;
    var $count = 0;
    var $error;
    var $i = 0;
    var $conn;

    public function __construct($conn) {
        $this->conn = $conn;
    }

    public function usersOnline () {
        $this->timestamp = time();
        $this->ip = $this->ipCheck();
        $this->new_user();
        $this->delete_user();
        $this->count_users();
    }

    public function ipCheck() {

        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public function new_user() {
        $insert = mysqli_query ($this->conn,"INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
        if (!$insert) {
            $this->error[$this->i] = "Unable to record new visitor\r\n";            
            $this->i ++;
        }
    }

    public function delete_user() {
        $delete = mysqli_query ($this->conn,"DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
        if (!$delete) {
            $this->error[$this->i] = "Unable to delete visitors";
            $this->i ++;
        }
    }

    public function count_users() {
        if (count($this->error) == 0) {
            $count = mysqli_num_rows ( mysqli_query($this->conn,"SELECT DISTINCT ip FROM useronline"));
            return $count;
        }
    }

}

?>

当你创建usersOnline的Object时,将$ conn变量作为参数传递给类似下面的参数:

使用代码/类

    $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost 
    $user = "my_user"; // your MySQL username 
    $pass = "mypassword"; // your MySQL password 
    $db = "my_db"; // the database to which you're trying to connect to


    //start database
     $conn = mysqli_connect($host,$user,$pass,$db);


    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }    
    $obj = new usersOnline($conn);

答案 1 :(得分:0)

public static GetAmountOfChips(): PromiseLike<any> {
    let chips: number = 1;
    NavigateTo.myProfile();
    browser.sleep(900);
    let promise = MyProfile.Basic.chipsAmount.getText().then((chipAmount) => {
        chips = parseInt(chipAmount);
        console.log("+++++++++++1" + chips);
    });
    MyProfile.Basic.close.click();
    console.log("+++++++++++2" + chips);
    return promise;
}

现在你的变量在类中,并且可以通过你拥有的任何其他函数全面使用。