0

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-->
4
  • 3
    Where are you accessing the database? You said you tried it and received errors. Please show the code! Commented Aug 30, 2013 at 7:39
  • I don't understand what is the problem here. Please explain more and show the errors you get. Commented Aug 30, 2013 at 8:43
  • If you’re saving your user data to a session, why can’t you then echo the user’s first name? What’s the problem? Commented Aug 30, 2013 at 9:35
  • 1
    You need to post your code that accesses database. Otherwise we cannot say, what is wrong with your code. Commented Aug 30, 2013 at 10:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Please do not recommend mysql_* - functions to the user. They are deprecated. Also your function uses $userid but does not declare it, neither is it passed as a parameter. Your code is also vulnerable for mysql injections. Really bad example!
@TobiasKun The userid variable is declared in the question above which is equal to $_SESSION['id']. I am using a function similar to this to display the current logged in user and other stats in my own website. I used mysql_ function by mistake..it should be mysqli_ function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.