Jump to content

Get Firstname & Lastname from Database?


Hazukiy

Recommended Posts

Hi, I'm just wondering but how would I go about making a query so that I can fetch two valid pieces of information and display them? So In this case I would like to query my database to find the correct Firstname and Lastname of that use through their session?

 

 

Here's something I made, it works! But it only fetches the first result and doesn't return the correct name for the correct user.

 

<?php
 
if ($stmt = $mysqli->prepare("SELECT id, firstname, lastname FROM users WHERE id LIMIT 1")) { 
      $stmt->execute();
      $stmt->store_result();
      $stmt->bind_result($user_id, $firstname, $lastname);
      $stmt->fetch();
 
      if($stmt->num_rows == 1) {
               $_SESSION['user_id'] = $user_id; 
               $_SESSION['firstname'] = $firstname;
               $_SESSION['lastname'] = $lastname;
			   
			   echo $firstname;
			   echo ' ';
			   echo $lastname; 
		}
}
?>

 

 

So at this point it's displaying two names but they are the first record in my database and it's not displaying the correct name for the correct user so for example.:

 

 

I'm user 6 in the database with the name John Doe but it's displaying record 1 in the database which is Luke Void, basically I want it so when John Doe logs into his account, it says on the screen 'John Doe', so it matches the correct client. Help on this one please, I'm kind of newish to databases... thanks :)

Link to comment
Share on other sites

in your existing thread for this, PaulRyan pointed out what you need to do to make it work :thumb-up:  -

 

 

When using "WHERE id = ?" you need to bind the id to ?.
 
Where is the user's id coming from? A session variable?

 

the query you posted in this thread is just testing where the id is a true value and returning the first row.

Link to comment
Share on other sites

in your existing thread for this, PaulRyan pointed out what you need to do to make it work :thumb-up:  -

 

 

 

the query you posted in this thread is just testing where the id is a true value and returning the first row.

 

Ah yeah sorry, still kind of getting my head around mySQL databases ect but how would I 'Bind' the 'id' to '?' Thanks. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.