我是PHP OOP的新手,我的代码有问题,并且不断出现错误:
致命错误:未捕获错误:调用E:\ xampp \ htdocs \ includes \ user_functions.php:35中未定义的方法DBConnection :: prepare()堆栈跟踪:#0 E:\ xampp \ htdocs \ includes \ process_login。 php(12):User :: login('admin','aa17a9b71574929 ...',Object(DBConnection))#1 {main}放在第35行的E:\ xampp \ htdocs \ includes \ user_functions.php中>
错误发生在prepare语句中,我似乎无法弄清楚我在做什么错。我似乎无法理解。我认为这与构造对象有关
我已经为我的连接创建了一个类,该类在我的登录脚本中调用。一旦进入脚本,我就会调用登录功能。似乎接受了连接,但是当我调用prepare()函数时出现了问题。
这是我的登录脚本:
<?php
include_once ('db_connect.php'); //Here i define my connection
include_once ('user_functions.php'); //In this file my functions
$mysqli = new DBConnection(); //i create a new object of the connection
User::sec_session_start(); // This is the script in my user_functions.php
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p'];
if (User::login($email, $password, $mysqli) == true) {
header('Location: ../index.php');
} else {
header('Location: ../error.php?error=1');
}
} else {
echo 'Invalid Request';
}
?>
这是我的db_connect.php
<?php
include_once ('psl-config.php'); // Da functions.php nicht included ist
class DBConnection {
protected $mysqli;
protected $db_host = HOST;
protected $db_username = USER;
protected $db_password = PASSWORD;
protected $db_name = DATABASE;
public function __construct() {
$this->mysqli= new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name)
or die($this->mysqli->error);
return $this->mysqli;
}
//$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); //This method worked previously, when not done with OOP
public function real_escape_string($str) {
return $this->mysqli->real_escape_string();
}
function __destruct() {
$this->mysqli->close();
}
}
?>
这是我的user_functions.php
<?php
require_once ('db_connect.php');
class User {
public function __construct($mysqli) {
return $this->mysqli=$mysqli;
}
function login($email, $password, $mysqli) {
if ($stmt = $mysqli->prepare("SELECT user_id, username, password, salt //This is where the error occurs
FROM users
WHERE email = ? OR username = ?
LIMIT 1")) {
$stmt->bind_param('ss', $email, $email);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $username, $db_password);
$stmt->fetch();
if ($db_password == $password) {
$user_browser = $_SERVER['HTTP_USER_AGENT'];
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
$username = preg_replace("/[^a-zA-Z0-9_\-]+/",
"",
$username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512',
$password . $user_browser);
return true;
} else {
return false;
}
}
}
}
?>
答案 0 :(得分:1)
return $this->mysqli;
这不起作用。看看这个:https://3v4l.org/OZjEl
您可以添加方法getConnection
或尝试扩展MySQLi类。
答案 1 :(得分:1)
因为您的DBConnection
变量中有$mysqli
个实例,而不是$this->mysqli
调用中的__construct()
个实例。尝试在您的DBConnection
类中创建单独的方法,该方法返回$this->mysqli
。像这样:
...
public function __construct() {
$this->mysqli= new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name)
or die($this->mysqli->error);
}
public function getConnection()
{
return $this->mysqli;
}
...
然后像这样$mysqli
:
$mysqli = (new DBConnection())->getConnection();
答案 2 :(得分:0)
尝试一下。
<?php
class DBConnection{
private $db;
public function __construct(){
try {
$this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo "Connection Successful";
}catch(PDOException $e){
echo $e->getMessage();
}
}
public function getConnection() {
if ($this->db instanceof PDO) {
return $this->db;
}
}
}
?>
然后创建类的实例并获取conn对象
$db_instance = new DBConnection();
$conn = $db_instance->getConnection();