Php 为什么我会犯致命错误?
下面是导致错误的代码片段Php 为什么我会犯致命错误?,php,sql,database,login,Php,Sql,Database,Login,下面是导致错误的代码片段 public function storeUser($email, $password) { require_once 'include/user.config.inc.php'; $uuid = uniqid('', true); //echo $uuid; $hash = $this->hashSSHA($password); //echo $hash; $encrypted_password = $hash
public function storeUser($email, $password) {
require_once 'include/user.config.inc.php';
$uuid = uniqid('', true);
//echo $uuid;
$hash = $this->hashSSHA($password);
//echo $hash;
$encrypted_password = $hash["encrypted"]; // encrypted password
//echo $encrypted_password;
$salt = $hash["salt"]; // salt
//echo $salt;
$query = "INSERT INTO user_table (unique_id, email_id, encrypted_password, salt, created_at) VALUES ( :uuid, :email, :encrypted_password, :salt, NOW()) ";
$query_params = array(
':uuid' => $uuid,
':email' => $email,
':encrypted_password' => $encrypted_password,
':salt' => $salt
);
try {
//These two statements run the query against the database table.
$stmt = $db -> prepare($query);
$result = $stmt -> execute($query_params);
} catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error! Problem with first query!";
die(json_encode($response));
}
}
我得到一个错误:
注意:未定义变量:db
致命错误:对非对象调用成员函数prepare()
似乎是$query和$query_参数导致了这个问题,但我不知道为什么。对我来说似乎是对的
下面是另一段代码
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = 2;
$response["error_msg"] = "User already exist";
echo json_encode($response);
} else {
$db->storeUser($email, $password);
}
您从未初始化$db对象 在“$stmt=$db->prepare($query);”之前需要类似于以下内容的内容 猜猜看
require_once
仅包括文件once,ever*。在其他地方之前,您可能已经需要了该文件,它不会再次被加载,您的$db
变量也找不到
*在当前脚本执行中。在哪里定义了$db
?PHP说它丢失了,因此失败了…它在include/user.config.inc.phpyes中初始化了。这就是问题所在,但现在我收到了这个错误“会话已经启动-忽略会话_start()”。由于我有两个函数在不同的场合访问数据库,如何结束上一个会话。我添加了调用这两个函数的代码
$db = new mydbclass();