使用PHP PDO /函数

时间:2019-03-09 15:10:08

标签: php pdo

我无法用下一个代码显示数据库中的其他行。当我输入<?php echo $_SESSION['username']; ?>时,我会在 home.php 上获得登录用户的用户名,但是当我想例如使用相同代码<?php echo $_SESSION['company']; ?>从数据库中显示用户公司时,我不会在 home.php 上获得该行。我的代码如下:

config.php

<?php
ob_start();
session_start();

date_default_timezone_set('Europe/London');

define('DBHOST','host');
define('DBUSER','user');
define('DBPASS','pass');
define('DBNAME','dbname');

define('DIR','localhost');
define('SITEEMAIL','email@host.com');

try {
    $db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}

include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

user.php

<?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 FROM tableName 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 login($username,$password){

            $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'];
                return true;
            }
        }

        public function logout(){
            session_destroy();
        }

        public function is_logged_in(){
            if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
                return true;
            }
        }
    }
?>

home.php

<?php
    require('includes/config.php');
    if(!$user->is_logged_in()){ header('Location: login.php'); }
?>

当我想显示用户信息时,请键入以下行代码:

<?php echo $_SESSION['username']; ?> **WORKING**

<?php echo $_SESSION['company']; ?> **NOT WORKING**

我认为问题在于某个功能,或者需要创建另一个功能来从数据库中检索所有字段,但是如果有人可以帮助我,那将是很棒的。谢谢大家

1 个答案:

答案 0 :(得分:0)

请参阅以下升级版的user.php。问题是,您没有从company中选择列db,也没有设置会话。

<?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, company FROM tableName WHERE username = :username AND active="Yes" '); //included the column company
            $stmt->execute(array('username' => $username));

            return $stmt->fetch();

        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }

    public function login($username,$password){

        $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['company'] = $row['company']; //Added this line
            return true;
        }
    }

    public function logout(){
        session_destroy();
    }

    public function is_logged_in(){
        if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
            return true;
        }
    }
}

?>