当我试图检索存储在name.php中的实体时,代码不起作用。它引发了上述错误。
name.php
<?php
session_start();
echo "Welcome " . $_SESSION["username"] . "! ";
if(isset($_POST['but_change'])) {
if($_SESSION["pass"] == $_POST['oldpwd']) {
$key= $datastore -> key('user', $_SESSION["id"]);
$entity = $transaction -> lookup($key);
echo $entity;
}
else echo 'User password is incorrect';
}
?>
login.php
$projectId = "s3764073-task2";
$datastore = new DatastoreClient([ 'projectId' => $projectId]);
$key = $datastore->key('user', $_POST['uname']);
$entity = $datastore->lookup($key);
$_SESSION["id"]= $_POST['uname'];
$_SESSION["username"] = $entity['name'];
$_SESSION["pass"] = $entity['password'];
根据"检索实体",我认为您错过了事务的初始化:
$transaction = $datastore->transaction();
我认为添加这个应该有效。
//--编辑根据github示例,您应该初始化数据存储客户端:
function initialize_client()
{
$datastore = new DatastoreClient();
return $datastore;
}