Call to a member function prepare() on a non-object
سلام دوستان.
ممنون میشم راهنمایی کنید.
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'db_connect.php';
$db = new DB_CONNECT();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email FROM users WHERE email = ?"); خطا به این خط اشاره میکنه
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
<?php
require_once 'db_config.php';
class DB_CONNECT
{
private $connection;
private $database;
function __construct() {
$this->connect();
}
function __destruct() {
$this->close();
}
function connect() {
$this->connection = mysqli_connect( DB_SERVER, DB_USER, DB_PASSWORD ) or die("cannot connect");
$this->database = mysqli_select_db($this->connection , DB_DATABASE ) or die("cannot select DB");
}
function close() {
mysqli_close( $this->connection );
}
}
?>
این کلاس DB_CONNECT هست
متد prepare رو باید روی آبجکتی صدا بزنید که این متد رو داشته باشه.
خروجی متد mysqli_connect همون آبجکت مورد نظره.
حالا شما توی کلاس DB_CONNECT یه متغیر به اسم connection دارید و درواقع روی این متغیر میشه اون متد رو صدا زد.ساده ترین راه حل اینه که توی کلاس DB_CONNECT ،این متغیرو بوسیله یک متد getter ، بهش دسترسی ایجاد کنید و بعدش توی کلاس DB_Functions مقدار متغیر conn رو بوسیله این متد getter برابر با connection قرار بدین تا از این به بعد بتونید روی این متغیر conn متد prepare رو صدا بزنید...
نکته : چون متد سازنده کلاس خروجی نداره(کلاس DB_CONNECT)،پس کدی که شما نوشته بودین عملا هیچ مقداری رو توی متغیر conn ست نمی کرد،یعنی متغیر conn توی کلاس شما مقدارش null هست و شما روی null ،اون متد prepare رو صدا زدین.
تغییرات لازم : (با انجام این کارا مشکل حل میشه،اما بهتره همه متد های مربوط به دیتابیس رو توی یه کلاس بنویسید.).
1- کلاس DB_Function :
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'db_connect.php';
$db = new DB_CONNECT();
$db->connect();
$this->conn = $db->getConnection();
}
}
2 - کلاس DB_CONNECT :
class DB_CONNECT
{
//...
private $connection;
private $database;
function getConnection() {
return $this->connection;
}
//...
}
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'db_connect.php';
// connecting to database
$db = new DB_CONNECT();
$db->connect();
$this->conn = $db->getConnection();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
<?php
require_once 'db_config.php';
class DB_CONNECT
{
private $connection;
private $database;
function getConnection() {
return $this->connection;
}
function __construct() {
$this->connect();
}
function __destruct() {
$this->close();
}
function connect() {
$this->connection = mysqli_connect( DB_SERVER, DB_USER, DB_PASSWORD ) or die (mysqli_error( $this->connection) . PHP_EOL);
$this->database = mysqli_select_db($this->connection , DB_DATABASE ) or die(mysqli_error( $this->connection) . PHP_EOL);
}
function close() {
mysqli_close( $this->connection );
}
}
?>
بازم،علت خطا،همون مشکل قبلیه اما این دفعه،متغیر stmt مقدار دهی نمیشه.
متد prepare به عنوان خروجی یه آبجکت برمی گردونه که میشه روش متد bind_param رو صدا زد(البته اگه با موفقیت و بدون خطا اجرا بشه).اما در صورتی که کد با خطا مواجه بشه،متد prepare مقدار false برمی گردونه و شما روی مقدار false ،متد bind_param رو صدا زدین.
علت false بودن متغیر stmt اینه که شما دستور sql رو توی متد isUserExisted اشتباه نوشتین.
نکته مهم : توی کدهایی که برای دانلود گذاشتین،این اشتباه رو انجام دادین،اما کدی که اینجا کپی شده درسته!!
بهر حال باید اینجوری بنویسید (بدون علامت ' ):
$stmt = $this->conn->prepare("SELECT email FROM users WHERE email = ?");
شما اینجوری نوشتین :
$stmt = $this->conn->prepare("SELECT 'email' FROM 'users' WHERE 'email' = ?");
کلاس Register
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password);
while($row=mysqli_fetch_array($user,MYSQLI_ASSOC)){
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $row["unique_id"];
$response["user"]["name"] = $row["name"];
$response["user"]["email"] = $row["email"];
$response["user"]["created_at"] = $row["created_at"];
$response["user"]["updated_at"] = $row["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
کلاس DB_Functions.php
<?php
class DB_Functions {
public function connectToDatabase(){
$connection = mysqli_connect("**, "**", "**", "**");
if (mysqli_connect_error()) {
echo "Connection failed: " . mysqli_connect_error();
}
return $connection;
}
public function storeUser($name, $email, $password) {
$connection= $this->connectToDatabase();
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = mysqli_prepare($connection,"INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
mysqli_stmt_bind_param($stmt,"sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
// check for successful store
if ($result) {
$stmt = mysqli_prepare($connection,"SELECT email FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt,"s", $email);
mysqli_stmt_execute($stmt);
$user = mysqli_stmt_bind_result($stmt, $email);
mysqli_stmt_close($stmt);
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$connection= $this->connectToDatabase();
$stmt = mysqli_prepare($connection,"SELECT email FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt,"s", $email);
if (mysqli_stmt_execute($stmt)) {
$user = mysqli_stmt_bind_result($stmt,$email);
mysqli_stmt_close($stmt);
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$connection= $this->connectToDatabase();
$stmt = mysqli_prepare($connection,"SELECT email FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt,"s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if (mysqli_stmt_num_rows($stmt) > 0) {
// user existed
mysqli_stmt_close($stmt);
return true;
} else {
// user not existed
mysqli_stmt_close($stmt);
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
کلاس login.php
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$email = $_POST['email'];
$password = $_POST['password'];
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
while($row=mysqli_fetch_array($user,MYSQL_BOTH)){
if ($user) {
// use is found
$response["error"] = FALSE;
$response["uid"] = $row["unique_id"];
$response["user"]["name"] = $row["name"];
$response["user"]["email"] = $row["email"];
$response["user"]["created_at"] = $row["created_at"];
$response["user"]["updated_at"] = $row["updated_at"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is missing!";
echo json_encode($response);
}
?>
موقع لاگین این پیغام میاد
البته موقع رجیستر هم همین پیغام خطا میاد اما در دیتابیس اطلاعات ذخیره میشه.
دوستان من قسمت register رو درست کردم بدون هیچ پیغام خطایی اما موقع لاگین کردن پیغام خطای زیر رو میده.
تغییرات
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $row["unique_id"];
$response["user"]["name"] = $row["name"];
$response["user"]["email"] = $row["email"];
$response["user"]["created_at"] = $row["created_at"];
$response["user"]["updated_at"] = $row["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
اما در اینجا متد مربوط به لاگین هنوز در کلاس DB_Functionمشکل داره :
public function getUserByEmailAndPassword($email, $password) {
$connection= $this->connectToDatabase();
$stmt = mysqli_prepare($connection,"SELECT email FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt,"s", $email);
if (mysqli_stmt_execute($stmt)) {
$user = mysqli_stmt_bind_result($stmt,$email);
mysqli_stmt_close($stmt);
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
مقادیر ایمیل و پسورد که در دیتابیس رجیستر شدن به درستی وارد میشن.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .




