我正在尝试使用oop类来获取用户信息,但我收到此错误
致命错误:在functions.php中的非对象上调用成员函数prepare()
user.php - >
include_once 'functions.php';
$user = new User();
echo $user->get_fullname(5);
functions.php - >
include_once 'database.php';
class User
{
public function connect()
{
$dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName = $dbh->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowName['emailaddress'];
return $email;
}
}
database.php - >
class DB_Class
{
public function connect() {
try {
$dbh= new PDO("mysql:host=localhost;dbname=root",'users','password', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
} catch (PDOException $e) {
echo $e->getMessage();
}
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
}
}
我做错了什么:(
答案 0 :(得分:1)
您实际上从未真正授予PDO
实例$dbh
对您尝试使用它的内容的访问权限。您似乎将类简单地用作函数分组,但也期望发生一些魔术: - )这就是我用你现有的代码做的事情:
class DB_Class {
protected $dsn;
protected $user;
protected $password;
protected $connection;
public function __construct($dsn = null, $user = null, $password = null)
{
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
}
public function connect()
{
if($this->connection) {
return $this->connection;
}
try {
$this->connection = new PDO($this->dsn,$this->user, $this->password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
$this->connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$this->connection->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
return $this->connection;
}
catch (PDOException $e) {
return false;
}
}
}
class User {
protected $db;
public function __construct(DB_Class $db = null)
{
$this->db = $db;
}
public function setDb(DB_Class $db)
{
$this->db = $db;
}
public function get_fullname($uid)
{
$stmt = $this->db->connect()->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$stmt->execute(array(':username', $uid));
if($row = $getName->fetch()) {
return $row['emailaddress'];
} else {
return null;
}
}
}
答案 1 :(得分:0)
$dbh
变量未传递到get_fullname
函数。你需要做类似的事情:
public function connect()
{
$dbh = new DB_Class();
return $dbh;
}
public function get_fullname($uid)
{
$dbh = $this->connect();
答案 2 :(得分:0)
您需要将$ dbh声明为User类的属性,并通过$ this identifier:
访问它class User
{
protected $dbh;
public function connect()
{
$this->dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName =$this->dbh->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowMail['emailaddress'];
return $email;
}
}
答案 3 :(得分:0)
您实际上从未将类属性指定为属性,在声明方法内部的变量将它们放入方法当前作用域时,您正在做的就是在方法完成执行后销毁它们。
要在类中正确设置属性,您需要使用$this->varname
声明它们,并且访问它们也是如此。
您还在为类构造使用了错误的名称,构造函数必须设置为__construct
而不是connect
。
正如您在更新的代码中所看到的,$dbh
变量现在是User类的属性,可以在它的方法之间调用。
用户类
class User
{
public function __construct()
{
$this->dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName = $this->dbh->getConnection()->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowName['emailaddress'];
return $email;
}
}
数据库类
class DB_Class
{
public function __construct() {
try {
$this->dbh = new PDO("mysql:host=localhost;dbname=root",'users','password', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
} catch (PDOException $e) {
echo $e->getMessage();
}
$this->dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$this->dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
}
public function getConnection() {
return $this->dbh;
}
}
使用http://us2.php.net/manual/en/oop5.intro.php作为资源,您将获益匪浅。