声明后的未识别变量

时间:2013-06-28 04:44:16

标签: php mysql

我收到此错误

Fatal error: Non-static method Connect::connect() cannot be called statically in D:\xampp\htdocs\Panel\core\init.php on line 63

这是我的代码

<?php
class Connect{
    public $db_host = "localhost";
    public $db_user = "root";
    public $db_pass = "";
    public $db_name = "panel";

    public function connect(){
            if(mysql_connect($db_host, $db_user, $db_pass)){
                    if(mysql_select_db($db_name)){
                            return true;
                    }else{
                            die(mysql_error());
                    }
            }else{
                    die(mysql_error());
            }
            return false;
    }
}
?>

如何使该功能保持静态? 我尝试在函数范围中添加'static',但是我收到了另一个错误

谢谢:)

3 个答案:

答案 0 :(得分:4)

您必须将变量传递给参数

function connect($db_host, $db_user, $db_pass,$db_name)

并将此功能称为

connect($db_host, $db_user, $db_pass,$db_name);

修改

通过查看您的pastebin,您正在调用类变量,您必须使用$this->variale_name来访问它们。

<?php
class Connect{
    public $db_host = "localhost";
    public $db_user = "root";
    public $db_pass = "";
    public $db_name = "panel";

    public function connect(){
            if(mysql_connect($this->db_host, $this->db_user, $this->db_pass)){
                    if(mysql_select_db($this->db_name)){
                            return true;
                    }else{
                            die(mysql_error());
                    }
            }else{
                    die(mysql_error());
            }
            return false;
    }
}
?>

PDO

<?php
class Connect{
    private $db_host = "localhost";
    private $db_user = "root";
    private $db_pass = "";
    private $db_name = "panel";
    private $dbh = false;
    public function connect(){
        if ($this->dbh === false)
            $this->dbh = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
        return $this->dbh;
    }
}
?>

答案 1 :(得分:1)

您必须在函数内声明变量,如此

<?php

function connect(){

    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "panel";

    if(mysql_connect($db_host, $db_user, $db_pass)){
        if(mysql_select_db($db_name)){

        }else{
            die(mysql_error());
        }
    }else{
        die(mysql_error());
    }
}
?>

或者您可以像这样传递函数中的参数

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";

connect($db_host,$db_user, $db_pass, $db_name);

function connect($db_host,$db_user, $db_pass, $db_name){


        if(mysql_connect($db_host, $db_user, $db_pass)){
            if(mysql_select_db($db_name)){

            }else{
                die(mysql_error());
            }
        }else{
            die(mysql_error());
        }
    }
    ?>

答案 2 :(得分:-1)

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";

这些变量是全局变量,无法在函数连接中访问。如果必须使用这些全局变量,请使用关键字global。然后这些变量将在函数中可用。

 function connect(){
        global $db_host, $db_user, $db_pass, $db_name ;
        if(mysql_connect($db_host, $db_user, $db_pass)){
            if(mysql_select_db($db_name)){

            }else{
                die(mysql_error());
            }
        }else{
            die(mysql_error());
        }
    }