Ok so I am able to create a user login system with data such as user ID, Username and a password.

Here's the scenario -


I add a field into the SQL table called 'Firstname'

How do I retrieve the 'Firstname' of every user which relates to their user ID and Username without displaying every firstname in the table ?

Thanks

Recommended Answers

All 5 Replies

you should add another UNIQUE filed which will act as primary key. Then you will use the primary key to get any record without conflict and database will not allow more than one same primary key

something like:
user_id, first_name, last_name, email

Thanks but I was going to use the user ID as the primary key. Sorry if that was mistaken. I just don't know how to write the PHP code to get the unique data that relates to the individual primary key.

how do you generate UseId?
EDIT:
If you use autoincrement, check this useful function

It's generated by the user signing up to the web site using a registration form..

Yes it would be auto-incremented, I just need an example code or explanation into how you retrieve the unique user data.

Member Avatar for diafol

This depends on whether you want to retrieve one at a time, all records that start with a letter (e.g. all usernames starting with a 'A') or if you use checkboxes to select a list of users.

Scenario 1: single retrieve
Get the $id you need from a POST or GET variable

$records = mysql_query("SELECT id, username, firstname FROM table WHERE id = '$id'");
if(mysql_num_rows($records) > 0){
  $data = mysql_fetch_array($records);
  echo "The username for this id is" . stripslashes($data['username']) . " and the firstname is " . stripslashes($date['firstname']);  
}else{
  echo "No users exist for that id value";
}

Scenario 2: multiple retrieve for usernames starting with a letter
Get the $letter you need from a POST or GET variable

$records = mysql_query("SELECT username, firstname FROM table WHERE username LIKE '{$letter}%'");
if(mysql_num_rows($records) > 0){
  echo "<ul>";
   while($data = mysql_fetch_array($records)){
      echo "<li>" . stripslashes($data['username']) . " (" . stripslashes($data['firstname']) . ")</li>";       
   }
  echo "</ul>";
}else{
  echo "No users exist with an username starting with $letter";
}

This code isn't tested so you might want to


Scenario 3: multiple retrieve for a number of unique ids
Get the $ids you need from a POST or GET variable (array from checkboxes)

$ids = implode(",",$_POST);

$records = mysql_query("SELECT username, firstname FROM table WHERE username IN ($ids)");
if(mysql_num_rows($records) > 0){
  echo "<ul>";
   while($data = mysql_fetch_array($records)){
      echo "<li>" . stripslashes($data['username']) . " (" . stripslashes($data['firstname']) . ")</li>";       
   }
  echo "</ul>";
}else{
  echo "No users exist with those ids";
}

I haven't tested this code - but I don't think it's far off the mark.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.