I went through all stakoverflow posts about the problem, and everywhere I find only some other procedure how to solve it. I am trying to store data to the database through android application, I had been doing that successfully until yesterday... Then I made some changes in the database and suddenly I cannot use my PHP anymore... Maybe the problem was that I added to column to database? Please help me if you can.
Here are my php files: DB_Connect.php:
<?php
class DB_Connect {
private $conn;
public function connect() {
require __DIR__ . "/Config.php";
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
return $this->conn;
}
}
?>
DB_Functions.php:
<?php
class DB_Functions {
private $conn;
function __construct() {
require __DIR__ . "/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, $facebook_json) {
$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, facebook_json, created_at) VALUES(?, ?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("ssssss", $uuid, $name, $email, $encrypted_password, $salt, $facebook_json);
$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
*/
public function getUserByEmail($email) {
$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();
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;
}
}
/**
* Updating facebook_json field
*/
public function updateFacebookJson($email, $facebook_json) {
$stmt = $this->conn->prepare("UPDATE users SET facebook_json = ?, updated_at = NOW() WHERE email = ?");
$stmt->bind_param("ss",$facebook_json, $email);
$stmt->execute();
$stmt->store_result();
}
/**
* Updating password for facebook_login first time
*/
public function updatePasswordFacebook($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();
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"];
$salt = $hash["salt"];
$stmt = $this->conn->prepare("UPDATE users SET encrypted_password=?, salt=?, updated_at = NOW() WHERE email = ?");
$stmt->bind_param("sss",$encrypted_password, $salt, $email);
$result = $stmt->execute();
$stmt->store_result();
$stmt->close();
if($result) {
return true;
} else {
return false;
}
} else {
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;
}
}
?>
register.php:
<?php
require_once __DIR__ .'/include/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'];
$facebook_json = NULL;
if(isset($_POST['facebook_json'])) {
$facebook_json = $_POST['facebook_json'];
}
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
if(!($facebook_json === NULL)) {
$db->updateFacebookJson($email, $facebook_json);
}
$user = $db->getUserByEmail($email);
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["facebook_json"] = $user["facebook_json"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password, $facebook_json);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["facebook_json"] = $user["facebook_json"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["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);
}
?>
Exact error: Uncaught Error: Call to undefined method mysqli_stmt::get_result() in register.php at this line: $user = $stmt->get_result()->fetch_assoc();