mattchambers
mattchambers

Reputation: 185

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-->

Upvotes: 0

Views: 2198

Answers (1)

Vikas Arora
Vikas Arora

Reputation: 1666

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

Upvotes: 1

Related Questions