我是 PHP 和 MYSQL 的 OOP 新手。我到处寻找解决方案,但找不到我正在寻找的东西。我正面临着
致命错误:在 integer 上调用成员函数 fetch_assoc() 。
如您所见,它表示一个整数。请让我知道我在哪里犯了错误。
- UserController 中的登录功能运行良好。
- 我为 fetch() 创建了一个单独的函数。
数据库配置.class.php
<?php
/** * Connects to Database */
class DatabaseConnect {
protected $db_host = 'localhost';
protected $db_name = 'procusa';
protected $db_user = 'root';
protected $db_pass = '';
private $conn;
public function __construct()
{
$this->conn = new mysqli($this-
>db_host,$this->db_user,$this->db_pass,$this->db_name);
if($this->conn->connect_error) {
header("Location: ../ErrorHandlers/DBError");
}
return true;
}
public function query($query)
{
$status = $this->conn->query($query);
if(!$status) return $this->conn->error;
else return 1;
}
public function fetch($result)
{
$row = $result->fetch_assoc();
if(!$fetch) echo "Could Not Fetch";
else return $row;
}
}
$con = new DatabaseConnect();
?>
用户控制器类.php
<?php session_start(); ?>
<?php
/**
* Class Template For Users
*/
class ProcusaUsers extends DatabaseConnect
{
function RegisterUser($password,$email,$phone,$username)
{
$reg_query = "INSERT INTO `registeredusers` (`id`, `email`, `password`,
`username`, `regd_time`)
VALUES (NULL, '".$email."', '".$password."', '".$username."', NOW());";
if( $result = DatabaseConnect::query($reg_query) ) {
$_SESSION['line'] = 1; //1 for ON Line
$_SESSION['email'] = $email;
header("location: index.php");
}
else
echo $result;
}
function AddMore($name) {}
function Login($email,$password) {
$login = "SELECT * FROM `registeredusers` WHERE email='$email' AND password = '$password'";
if($status = DatabaseConnect::query($login) ) {
$_SESSION['line'] = 1;
$r = DatabaseConnect::fetch($status);
return $r;
} return false;
}
}
$user = new ProcusaUsers();
?>
登录.php
<?php
if (isset($_POST['signin'])) {
if($result = $user->Login($_POST['email'] , $_POST['password'])) {
$row = $con->fetch_assoc($result);
print_r($row);
}
}
?>