-5

我收到此错误

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

如何使函数静态?我尝试将“静态”添加到函数范围,但出现另一个错误

谢谢 :)

4

3 回答 3

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;
    }
}
?>
于 2013-06-28T04:47:17.417 回答
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());
        }
    }
    ?>
于 2013-06-28T04:53:24.747 回答
-1
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";

这些变量是全局的,不能在函数 connect 中访问。如果您必须使用这些全局变量,请使用关键字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());
        }
    }
于 2013-06-28T04:53:35.790 回答