2

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.

1
  • You really should look into PDO. The procedural mysql_* API is more the 10 years old. You should not write new code with it. Commented Mar 14, 2012 at 19:38

1 Answer 1

6

You misspelled __construct so it's not firing when you initialize the class, and not initializing $this->con

Sign up to request clarification or add additional context in comments.

4 Comments

How embarassing thank you I have stared at this for a while can't belive I missed it.
Anything else you see would be appreciated though this is my first class and feedback is alwayas good.
Only other error I notice is $result = mysql_query($query) or queryErrorReport($query, __LINE__); should be $this->queryErrorReport(). Other than that, it looks fine, and if it runs, then you have a good class. :)
You also typo'ed and put $this->$con, instead of $this->con

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.