简体   繁体   中英

How Do I show a users Email address from MYSQL?

I'm trying to get it where, when the user goes to their profile, it will show them their email address. I have it where it shows their username by using $_SESSION['username'] but when I try that for the EMAIL address it doesn't work. Any Ideas on how I can get it to SELECT the email address from the Database? Thank You In Advance!! :)

电子邮件地址: It's showing their current Email address: and then the code. Here's my PHP:

Includes/config.php is getting the DATABASE INFO.

<?php require('includes/config.php'); 

//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php?action=login'); } 

 ?>

Includes/config.php

<?php
ob_start();
session_start();

//set timezone
date_default_timezone_set('Europe/London');

//database credentials
define('DBHOST','localhost');
define('DBUSER','xxxxx');
define('DBPASS','xxxxx');
define('DBNAME','bigfellamembers');

//application address
define('DIR','http://xxxxxxxxxxx.com');
define('SITEEMAIL','noreply@domain.com');

try {

    //create PDO connection
    $db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

//include the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

Here's my database table. 数据库 datbase

Heres: some of my HTML:

   <!-- row -->
                <div class="row">
                    <div class="col-md-12">
                        <div class="white-box">
                            <div class="user-bg"><img width="100%" alt="user" src="login-bg.jpg">
                                <div class="overlay-box">
                                    <div class="user-content">
                                        <a href="javascript:void(0)"><img src="uploads/<?php echo $_SESSION['username']; ?>.jpg" class="thumb-lg img-circle" alt="img"></a>
                                         <h4 class="text-white">Current Username: <?php echo $_SESSION['username']; ?> </h4>
                                        <h5 class="text-white">Current Email Address:    <?php  echo $_SESSION['email']; ?>

                                   </div>
                                </div>
                            </div>

If you haven't set the email within the SESSION then you can't try and grab it like that.

You'll need to use a MySQL query (research PDO) to grab the email from the database based on some criteria (eg username).

You first need to get the user Email from database, Let's say

Query:

"SELECT email from user WHERE username ='$username' ";

After running the query and fetching your data from database, you can then do these: $_SESSION['email'] = $result['email'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM