Sepax1
شنبه 29 فروردین 1394, 23:32 عصر
سلام یه همه دوستان .
من یه قطعه کد دارم که به وسیله pdo به دیتابیس وصل میشه ولی الان یه ایرادی داره .
<?php
class DB_Functions {
// private $db;
//put your code here
// constructor
function __construct() {
// require_once 'db_conncet.php';
// // connecting to database
// $this->db = new DB_Connect();
// $this->db->connect();
require_once 'config.php';
$username = 'netshope_sepehr';
$password = 'sepax963258';
// connecting to mysql
try {
$db = new PDO('mysql:host=localhost;dbname=netshope_login',$ username , $password);
$db->setAttribute(PDO::ATR_ERRMODE , PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Error" . $e->getMessage() ;
}
}
// 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
// $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at)
// VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
try {
$stmt = $db->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at)
VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())");
$stmt->execute(array(
':uuid' => $uuid,
':name' => $name,
':email' => $email,
':encrypted_password' => $encrypted_password,
':salt' => $salt
));
if($stmt->rowCount() > 0){
$uid = mysql_insert_id();
$result = $db->prepare("SELECT * FROM users WHERE uid = :uid");
$result->execute(array(
':uid' => $uid
));
return $result->fetchAll();
} return false ;
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
} // 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
// $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) // VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
try {
$stmt = $db->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())"); $stmt->execute(array( ':uuid' => $uuid, ':name' => $name, ':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt ));
if($stmt->rowCount() > 0){ $uid = mysql_insert_id();
$result = $db->prepare("SELECT * FROM users WHERE uid = :uid"); $result->execute(array( ':uid' => $uid ));
return $result->fetchAll();
} return false ;
} catch (PDOException $e) { echo 'ERROR: ' . $e->getMessage(); }
}[/PHP]
وقتی از تو index.php میخوام یه از این کلاس استفاده کنم نمیشه و ارور زیر رو میده :
Fatal error</b>: Call to a member function prepare() on a non-object
index.php
<?php
/**
* File to handle all API requests
* Accepts GET and POST
*
* Each request will be identified by TAG
* Response will be JSON data
* check for POST request
*/
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = TRUE;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
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"]["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"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>
من یه قطعه کد دارم که به وسیله pdo به دیتابیس وصل میشه ولی الان یه ایرادی داره .
<?php
class DB_Functions {
// private $db;
//put your code here
// constructor
function __construct() {
// require_once 'db_conncet.php';
// // connecting to database
// $this->db = new DB_Connect();
// $this->db->connect();
require_once 'config.php';
$username = 'netshope_sepehr';
$password = 'sepax963258';
// connecting to mysql
try {
$db = new PDO('mysql:host=localhost;dbname=netshope_login',$ username , $password);
$db->setAttribute(PDO::ATR_ERRMODE , PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Error" . $e->getMessage() ;
}
}
// 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
// $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at)
// VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
try {
$stmt = $db->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at)
VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())");
$stmt->execute(array(
':uuid' => $uuid,
':name' => $name,
':email' => $email,
':encrypted_password' => $encrypted_password,
':salt' => $salt
));
if($stmt->rowCount() > 0){
$uid = mysql_insert_id();
$result = $db->prepare("SELECT * FROM users WHERE uid = :uid");
$result->execute(array(
':uid' => $uid
));
return $result->fetchAll();
} return false ;
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
} // 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
// $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) // VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
try {
$stmt = $db->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())"); $stmt->execute(array( ':uuid' => $uuid, ':name' => $name, ':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt ));
if($stmt->rowCount() > 0){ $uid = mysql_insert_id();
$result = $db->prepare("SELECT * FROM users WHERE uid = :uid"); $result->execute(array( ':uid' => $uid ));
return $result->fetchAll();
} return false ;
} catch (PDOException $e) { echo 'ERROR: ' . $e->getMessage(); }
}[/PHP]
وقتی از تو index.php میخوام یه از این کلاس استفاده کنم نمیشه و ارور زیر رو میده :
Fatal error</b>: Call to a member function prepare() on a non-object
index.php
<?php
/**
* File to handle all API requests
* Accepts GET and POST
*
* Each request will be identified by TAG
* Response will be JSON data
* check for POST request
*/
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = TRUE;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
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"]["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"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>