آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

Call to a member function prepare() on a non-object

فرشاد  9 سال پیش  9 سال پیش
0 0

سلام دوستان.

ممنون میشم راهنمایی کنید.

 <?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;
}

}

?>




0 0
این خطا زمانی رخ میده که sql که نوشتی اشتباه باشه. چک کن ببین کجاش اشتباهه (9 سال پیش)
0 0
همیشه برای اطمینان sql رو توی phpmyadmin کپی کن و اجراش کن ببین کار میکنه یا نه. (9 سال پیش)
0 0
ممنون از جوابتون.مشکل همینجاست خط به خط رو چک کردم و توی تمام سایتا این ارور رو سرچ کردم اما نتونستم مشکل رو متوجه بشم. (9 سال پیش)
0 0
سلام.متد connect از کلاس DB_CONNECT چه چیزی رو برمی گردونه؟ این متد باید یه آبجکتی برگردونه که متد prepare بتونه روش اجرا بشه . (9 سال پیش)
0 0
(9 سال پیش)
 برای این سوال 6 پاسخ وجود دارد.
پاسخ به سوال 
فرشاد  9 سال پیش
0 0

 <?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 هست
0 0
درسته،مشکل کار از همین کلاس DB_CONNECT هستش.این کلاس رو بدرستی پیاده سازی نکردین.قبل از اینکه من جواب بدم،خودتون یه بار دیگه سعی کنید، ببینید می تونید مشکل رو حل کنید.اگه به جواب نرسیدین من سعی می کنم،صبح جواب رو براتون بنویسم.اگه خودتون به جواب برسین خیلی بهتره... (9 سال پیش)
0 0
باشه مرسی . لطف می کنید . البته من همیشه تمام سایتا مخصوصا stackoverflow رو چک میکنم اما اینجا یه جورایی به بن بست رسیدم. این لینک فایل های php هست. لینک (9 سال پیش)
0 0
لینک لینک قبلی مشکل داشت (9 سال پیش)
پاسخ به سوال 
sami  9 سال پیش
0 0

متد 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;
}
//...

}
 
 
0 0
ممنون از وقتی که گذاشتید دوست عزیز. کدها رو اضافه کردم و این بار با خطای دیگه مواجه شدم.در قسمت جواب درج می کنم. (9 سال پیش)
پاسخ به سوال 
فرشاد  9 سال پیش
0 0

 <?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 );
}
}

?>
پاسخ به سوال 
sami  9 سال پیش
0 0

بازم،علت خطا،همون مشکل قبلیه اما این دفعه،متغیر 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' = ?");

 

0 0
ممنون . اعمال کردم و کدها رو هم به کل تغییر دادم.یعنی به روش Procedural نوشتم و خوشبختانه تونستم رجیستر کنم(هنوز کمی مشکل داره) . اما قسمت لاگین با این خطا مواجه میشم (در قسمت جواب می نویسم). (9 سال پیش)
پاسخ به سوال 
فرشاد  9 سال پیش
0 0

کلاس 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);
}
?>


موقع لاگین این پیغام میاد
البته موقع رجیستر هم همین پیغام خطا میاد اما در دیتابیس اطلاعات ذخیره میشه.
0 0
اگه یه کم به پیغام خطایی که داده،توجه کنید براحتی می تونید مشکل رو حل کنید!ضمنا اینجا انجمن اندرویده بهتره این جور سوالارو توی انجمن وب مطرح کنید. (9 سال پیش)
0 0
درست می گید. اما چون مربوط به پروژه اندروید مارکت هست گفتم شاید بتونم اینجا مطرح کنم. در رابطه با اسون بودن راه حل هم بنده خودم بعد از کلی ازمون و خطا کردن مجبور میشم اینجا سوال بپرسم و توی این برهه منتظر راه حل شما هستم. مرسی دوستان (9 سال پیش)
پاسخ به سوال 
فرشاد  9 سال پیش
0 0

دوستان من قسمت 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;
}

}


مقادیر ایمیل و پسورد که در دیتابیس رجیستر شدن به درستی وارد میشن.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .