简体   繁体   中英

OOP PHP First Class Mysql database connection

I am working on a test class mostly to self teach. Below is the class:

class Connection
{   
    public $con;
    public $dbSelected;
    public $activeConnection;
    public $dataBaseName;
    function __contruct($dbUserName, $dbPassword, $server = "localhost")
    {
        $this->con = mysql_connect($server,$dbUserName,$dbPassword);
        if(!$this->$con)
        {
            $this->activeConnection = false;
        }
        else
        {
            $this->activeConnection = true;
        }
    }

    public function dbConnect($dbName, $identifyer = null)
    {   
        if ($identifyer === null)
        {
            $identifyer = $this->con;
        }
        $this->dbSelected = mysql_select_db($dbName, $identifyer);
        $this->dataBaseName = $dbName;
        if($this->dbSelected != true)
        {
            $this->connectionErrorReport(__LINE__);
        }
    }


    public function cleanData( array $submission)
    {
        unset($submission["throughTheCleaners"]);
        foreach($submission as $key => $value)
        {

            if(is_array($value))
            {
                $data[$key] = $this->cleanData($value);
            }
            else
            {
                $data[$key] = mysql_real_escape_string($value);
            }
        }
        $data["throughTheCleaners"] = true;
        return $data;
    }


    public function query($query, $dataSent)
    {
        if($dataSent["throughTheCleaners"] != true)
        {
            die("you must clean the data".__LINE__);
        }

        if($this->activeConnection == true && $this->dbSelected == true)
        {
            $result = mysql_query($query) or queryErrorReport($query, __LINE__);
            $i = 0;
            while($row = mysql_fetch_array($result))
            {
                foreach($row as $key => $value)
                {
                    $data[$i][$key] = $value;
                    $i++;
                }
            }
            return $data;           
        }
        else
        {
            $this->$connectionErrorReport(__LINE__);
        }
    }   

    public function connectionErrorReport($line = __LINE__)
    {
        $error = "There has been a connection error on line ".$line."</br>";
        if($this->activeConnection == false)
        {
            $error.= "Active Connection Error <br/>";
        }
        if($this->dbSelected  == false)
        {
            $error.= "Data Base Selection Error <br/>";
        }
        die($error.mysql_error());
    }

    public function queryErrorReport($query, $line = __LINE__)
    {
        die("There was a query error on ".$line."<br />$query<br/>".mysql_error());
    }

    function __destruct() {
        mysql_close($this->con);
    }
}

I can not for the life of my figure out why it will not pick up the mysql database resourse stored in the $con variable during the contructor call and use it in other functions.

Eventually there will be an encryption function for strings in another class but the only errors I am currently getting fall on the connection class.

So the page throwing the error looks like this:

include 'connection.php';
include 'loginClass.php';
$longinConnection = new Connection('***USERNAMEHERE***','***PASSWORD***');
$longinConnection->dbConnect("***DBTOCONNECTTO***");
echo Authoriz::encryptPassword("***USERPASSWORD***",$longinConnection);

This page tests the encryptPassword function but I can't even get the Connection class to work. The errors it throws are below:

Warning: mysql_select_db() expects parameter 2 to be resource, null given in [PATH] on line 27 There has been a connection error on line 31 Active Connection Error Data Base Selection Error

Warning: mysql_close() expects parameter 1 to be resource, null given in [PATH] on line 103

It seems to me that the $con variable is not being set for some reason any insight into what I am doing wrong would be greatly appreciated.

您拼写了__construct ,使它在初始化类时没有触发,并且没有初始化$this->con

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM