在mainpage.php文件中,我用这个:
<?php
$dsn = 'mysql:host=localhost;dbname=webfilter_schema';
$username = 'root';
$password = '';
$dbh = new PDO($dsn, $username, $password); //WORKS.
和它的罚款。但是在另一个php文件中:
<?php
class HomeController {
public $pdoObject; // handle of the db connexion
private static $instance;
public function __construct()
{
$dsn = 'mysql:host=localhost;dbname=webfilter_schema';
$user = "root";
$password = "";
$this->$pdoObject = new PDO($dsn, $user, $password);// Error line..
}
public function createLocalObject(){
$query ="INSERT INTO USERS SET NAME = ?, PASSWORD = ?,IPADDRESS=?,E_MAIL=?";
$process = $this->pdoObject->prepare($query);
$insertResult = $process->execute(array("asd","ferfr","23","sadsads@hotmail.com"));
if($insertResult)
{
return true;
}
return false;
}
}
?>
它抛出了像
这样的异常无法在第25行的C:\ xampp \ htdocs \ WP \ Controller \ HomeController.php5中访问空属性
这是怎么回事?
答案 0 :(得分:2)
只是将语言添加为参数..
public function __construct() {
$dsn = 'mysql:host=localhost;dbname=webfilter_schema';
$username = 'root';
$password = '';
$options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', );
$this->pdoObjectp = new PDO($dsn, $username, $password, $options);
}
它有效。