如何在php中回显mysql当前会话中的数据?

kyvafyod  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(268)

在我开始之前,我必须说我是php的大傻瓜。我正在学习如何使用mysql创建登录/注册php页面的在线教程:https://daveismyname.blog/login-and-registration-system-with-php
我添加了自己的sql列,名为“investment”,作为int(11)。
我可以回显会话用户名,但不能回显会话投资。
“投资”列的默认值为“0”不确定这是否重要,但仍要声明它。
当回声被称为点的数量应该是空的。
index.php(投资回音):

<a href="#" class="dropdown-toggle waves-effect waves-button waves-classic fa fa-usd" data-toggle="dropdown"> Invested Amount: $
  <?php echo $_SESSION['investment'];?>
  <i class="fa fa-angle-down">
  </i>
</a>
<ul class="dropdown-menu title-caret dropdown-lg" role="menu">
  <li class="drop-all">
    <a href="#" class="text-center">Portfolio
    </a>
  </li>
  <li class="drop-all">
    <a href="#" class="text-center">Invest
    </a>
  </li>
</ul>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle waves-effect waves-button waves-classic" data-toggle="dropdown">
    <span class="user-name">
      <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES); ?>
      <i class="fa fa-angle-down">
      </i>
    </span>
  </a>

代码处理会话:

<?php
include('password.php');
class User extends Password{
    private $_db;
    function __construct($db){
        parent::__construct();
        $this->_db = $db;
    }
    private function get_user_hash($username){
        try {
            $stmt = $this->_db->prepare('SELECT password, username, memberID, investment,email FROM members WHERE username = :username AND active="Yes" ');
            $stmt->execute(array('username' => $username));
            return $stmt->fetch();
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }
    public function isValidUsername($username){
        if (strlen($username) < 3) return false;
        if (strlen($username) > 17) return false;
        if (!ctype_alnum($username)) return false;
        return true;
    }
    public function login($username,$password){
        if (!$this->isValidUsername($username)) return false;
        if (strlen($password) < 3) return false;
        $row = $this->get_user_hash($username);
        if($this->password_verify($password,$row['password']) == 1){
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $row['username'];
            $_SESSION['memberID'] = $row['memberID'];
            $_SESSION['email'] = $row['email'];
            $_SESSION['investment'] = $row['investment'];
            return true;
        }
    }
    public function logout(){
        session_destroy();
    }
    public function is_logged_in(){
        if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
            return true;
        }
    }
}
?>
zujrkrfu

zujrkrfu1#

下面是一个例子。调用当前id会话,然后在数据库中选择。您不必会话所有数据。

$crud = new crud($DB_con);

$id = $_SESSION['id'];

$stmt = $crud->runQuery("SELECT * FROM tbl_login WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
echo $userRow['investment'];

相关问题