Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:02:25+00:00 2026-06-13T10:02:25+00:00

In my table ‘user_sightings’ I allow multiple entries for each user, so it looks

  • 0

In my table ‘user_sightings’ I allow multiple entries for each user, so it looks as follows…

------------------------
ID | user_ID | postcode
1     39       ab12cd
2     39       sk91ab
3     39       ab44ix

After running my query I try to print out each postcode with the following…

echo $row["postcode"];

only this only prints out the first instance for that user, Is their a way I can get all instances for the user and dynamically set them as variables?


Query:

$sth = $conn->prepare("SELECT * FROM directory, user_sightings WHERE directory.user_active != '' AND directory.ID = :uid AND user_sightings.user_ID = :uid");
$sth->execute(array(':uid' => $UID));

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
      echo $row["postcode"];
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:02:26+00:00Added an answer on June 13, 2026 at 10:02 am

    You need a while loop to fetch the rows, then you can use variable assignment in PHP to assign a variable to each user:

    while($row ... {
        ${$row[user_ID]} = $row[postcode];
    }
    

    You will need a way to uniquely identify the variables, because then the last instance of that user_ID will be the value of that variable I would recommend using an array. Perhaps something like:

    ${'user_' . $row[user_ID]}[] = $row[postcode];
    

    This way, $user_39 will become an array containing (ab12cd, sk91ab, ab44ix).

    Or you can throw all your data into an array like so:

    $user_ID = $row[user_ID];
    $array[$user_ID][] = $row[postcode];
    

    Either of those two options would go inside your while loop.

    Clarification

    If you want to call the postcode for a particular user, first put the data into a multi-dimensional array:

    while($row ... {
        $user_ID = $row[user_ID];
        $array[$user_ID][] = $row[postcode];
    }
    

    The postcodes will then be stored like so (following example in question):

    echo $array[39][0];
    //ab12cd
    
    echo $array[39][1];
    //sk91ab
    
    echo $array[39][2];
    //ab44ix
    

    [39] represents the user_ID

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

table: user | userid | fullname | | 1 | admin | | 2
Table argument_user +-------------+----------+ | argument | user_id | +-------------+----------+ | bike | 1 |
TABLE users_community_relations list_id int user_id int dest_user_id int 1 10 20 2 10 24
table A amount user_id 100 abc 200 cdf 300 def table B Idno user_id
Table Each row represents a video that was on air at particular time on
Table A has a number of rows, only some of which have (multiple) children
Table Name: MRM_BOOKING_DETAILS Sample Data: Booking_id USER_ID ROOM_ID CHECKIN CHECKOUT 1 hary 1 2012-04-16
table : id (integer primary key) data (blob) I use mysql and sqlalchemy. To
Table --------------------------------------------- | id | first_name | middle_name | last_name | --------------------------------------------- | 1
Table structure id (p) | date | id2 (fk) | id3 (fk) where (p)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.