我一直在学习关于php的oop,我一直在尝试创建一个类,我可以使用$ data-> uname从数据库中获取信息; 但是当我创建我的课程时,我得到了一个
( ! ) Notice: Undefined property: UserData::$uname in C:\wamp\www\header.php on line 264
错误
header.php第264行只是
<?php $data->uname; ?>
我的课程从文件顶部开始
$data = new UserData($_SESSION['id']);
classfile是
<?php
require_once("functions/connect.php");
class UserData{
public $db;
public function __construct(){
$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(mysqli_connect_errno()){
echo "Could not establish connection";
exit;
}
}
function data($id){
$stmt = $this->db->prepare("SELECT * FROM users WHERE uid = ?");
$stmt->bind_param("i",$id);
$stmt->execute();
$result = $stmt->get_result();
$users = $result->fetch_assoc();
foreach ($users as $obj => $value){
$this->$obj = $value;
}
}
}