简体   繁体   中英

PHP/MYSQL echo logged in user's data from database table

can anyone help me with the situation I'm in. I've been trying to find answers on how to display a user's first name, last name email...etc from the table that has the info stored but my only luck with echoing this info is only getting either ALL the info for a certain feel to show like all emails instead of just 1 specific email for the user or I just end up with mysql errors.

here's my index.php source

<?php

session_start();

if (isset($_SESSION['id'])) {
  // Put stored session variables into local php variable
  $userid = $_SESSION['id'];
  $email = $_SESSION['email'];
  $firstname = $_SESSION['first_name'];
  $lastname = $_SESSION['last_name'];
  $businessname = $_SESSION['company_name'];
  $country = $_SESSION['country'];
  $plan = $_SESSION['plan'];
} else {
    header("Location: http://somewebsite.com");
}   

include 'connect.php';

$first_name = $_GET['first_name'];
$last_name = $_GET['last_name'];

?>

for instance I would like to echo the user's first name in the header

<span class="username">USER</span> <--replace user with logged in user's firstname n last        name-->

You can do something like this: Write a function to fetch specific data from the table such as one below.

function getuserfield($field) {
    $query = "SELECT $field FROM users WHERE id='".$userid."'";
    if ($query_run = mysqli_query($query)) {
        if ($query_result = mysqli_result($query_run, 0, $field)) {
            return $query_result;
        }
    }
}
/* userid = $_SESSION['id']; */

The "$query_result" will display the username. $field is the specific field you want to display. By this function you can fetch any particular data from the specified field

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