0

The application I'm building requires some users to be linked to eachother.

There will be users and mentors. The database field "mentor_link" has a value. This value determines which users the mentor is linked to.

What I'm trying to do is search the database for the users with a certain "mentor_link" value and save their usernames as php variables to be used later.

Using json_encode() AND print_r simply displays something that looks like this:

[{"userName":"dave","0":"dave"},{"userName":"ely","0":"ely"},{"userName":"mentor1","0":"mentor1"}]

What I'm looking for is to target each username and save it as a variable. For example: $user1 = dave; $user2 = ely; etc etc.

All the other examples I've found only allow me to display it the same way as above.

My code is as follows:

$stmt = $user_home->runQuery("SELECT userName FROM tbl_users WHERE mentor_link=101");
$stmt->execute();

$result = $stmt->fetchAll();
echo json_encode($result);

The database is connected fine and I'm able to pull single values based on the users details but I can't figure out how to get info on other users and display it/save it as a variable.

Oh and I'm using PDO to connect to the database.

2
  • Why create a php variable from each user in your table? :O Commented Jul 4, 2016 at 22:38
  • Sorry, still getting used to asking questions on here and I'm not so great at putting my point across. WHY I'm doing it is because I'm building a function where mentors can chat to their users. I need to use the usernames of the users they're connected to allow them to select which users to chat to. If that makes sense? Commented Jul 4, 2016 at 22:44

1 Answer 1

1

After this line:

$result = $stmt->fetchAll();

You can do anything with that data

print_r($result[0]);//user1
print_r($result[1]);//user2

or

$user1 = $result[0]; 
$user2 = $result[1];

then

echo $user1['userName'];//dave
echo $user2['userName'];//ely
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! You sir are my saving grace today! Thanks! I really should've seen that. I'm fairly new to PHP

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.