无法访问函数中的类变量

时间:2016-11-01 03:25:46

标签: php mysql html5 mysqli

我正在尝试使用加密创建数据库。我正在制作一个将数据插入数据库的PHP脚本。

我无法检索任何类变量(特别是$this->decryptionKey

class DBHandler {
public $decryptionKey = "hi";
public $dbhost;
public $dbuser;
public $dbpass;

function DBHandler(){
    echo $this->decryptionKey;
    $this->decryptionKey = "PASSWORD";
    echo $this->decryptionKey;
    $this->dbhost = 'localhost:3306';
    $this->dbuser = 'root';
    $this->dbpass = 'password';
}

function insertStudent($id, $fName, $lName, $email, $phone){
    //connect to database
    echo $this->decryptionKey;
    echo "hello";
    $link = mysqli_connect('localhost:3306', 'root', 'password', "", 3306);




    echo "world";

    //define sql code
    $sql = "INSERT INTO `Checkout_System`.`STUDENT` " . "(`STUDENT_ID`, `STUDENT_FNAME`, `STUDENT_LNAME`, `STUDENT_EMAIL`, ". "`STUDENT_PHONE`, `STUDENT_OFFENSE_COUNT`) " . "VALUES (" . $id . ", AES_ENCRYPT(\'" . $fName . "\', \'" . $this->decryptionKey. "\'), AES_ENCRYPT(\'" . $lName . "\', \'" . $this->decryptionKey . "\'), AES_ENCRYPT(\'" . $email . "\', \'" . $this->decryptionKey . "\'), AES_ENCRYPT(\'" . $phone . "\', \'" . $this->decryptionKey. "\'), 0);";

    echo "hello!!!!!";

    //execute query
    $retval = mysqli_query($link, $sql);


    //check success
    if(!$retval){
        die('Failure:' . mysql_error());
        echo "<script>console.log(\"failure\")</script>";
    }
    else{
        echo "<script>console.log(\"failure\")</script>";
    }


}

function template($id, $fName, $lName, $email, $phone){
    //connect to database
    $link = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass);

    //define sql code
    $sql = "";

    //execute query
    $retval = mysqli_query($link, $sql);


    //check success
    if(!$retval){
        die('Failure:' . mysql_error());
        print("failure");
    }
    else{
        print("SUCCESS!");
    }
}   
  }

输出结果如下:

hiPASSWORD

我不知道为什么我能够在一个“构造函数”函数中访问变量,而不是另一个函数。我试着为变量做一个getter,但那也失败了。我无法使用NetBeans的调试功能,因为它不想使用现有的NetBeans调试工具。

0 个答案:

没有答案