0

I have a table in the following structure...

ID     USER_ID      INTEREST
1      290          soccer
2      290          tennis
3      290          badminton
4      560          netball

I want to grab all values from the table where the user id is equal to the session user id and assign them as variables.

I have the following which works but displays the data very peculiarly..

$interestsquery  = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
 $interests[] = $row['interest'];
 $interest1 = $interests[1];
 $interest2 = $interests[2];
 $interest3 = $interests[3];

 print $interest1 . " - " . $interest2 . " - " . $interest3;

} 

The above however outputs something along the lines of...

- - tennis - - tennis - badminton - 

Can anybody see where I'm going wrong?

2
  • What is the structure of your table and what are you trying to do? Commented Jul 7, 2011 at 8:20
  • I want to grab all values from the table where the user id is equal to the session user id and assign them as variables. Commented Jul 7, 2011 at 8:21

3 Answers 3

2

This should do what you need:

$interestsquery  = "SELECT `interest` FROM `user_interests` WHERE `user_id` = " . $usersClass->userID();
$result = mysql_query($interestsquery);

$interests = array();
while(list($interest) = mysql_fetch_array($result))
    $interests[] = $interest;

print explode($interests, ' - ');
Sign up to request clarification or add additional context in comments.

2 Comments

ah, you should just do this then: echo '<pre>' . print_r($interests,true) . '</pre>';
Thanks Andy, Is it possible however to assign the results as a variable? So... $interest1 = Soccer; $Interest2 = Tennis; etc etc...
1

Think what happens to your loop at the first iteration:

  • $interest is an empty array
  • you fetch the first value and put it into $interest[0],
  • you fill $interest1 with the value that lies into $interest[1] (it is empty)
  • same for $interest2 and $interest3
  • you print ""." - ".""." - ".""

in the second run:

  • $interest is [0=>soccer]
  • you fetch the second value and put it into $interest[1],
  • you fill $interest1 with the value that lies into $interest[1] tennis
  • same for $interest2 and $interest3 (that are still empty)
  • you print "tennis"." - ".""." - ".""

and so on.

You need print the result when you exit the while loop (and the code is still flawed as it don't get the value into the index 0 of the array).

An alternative should be:

$interestsquery  = "SELECT `interest` FROM `user_interests` WHERE `user_id` = " 
                 . $usersClass->userID();
$result = mysql_query($interestsquery);

$interests = array();
// you fetch just a field, fetch_row will be sufficent
while($interest = mysql_fetch_row($result)) {
    array_push($interests, $interest[0]);
}
echo implode(' - ', $interests);

Comments

0
$interests = array();

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  $interests[] = $row['interest'];
}

$int_count = count($interests);
$i=0;

foreach ($interests as $interest) {
    $var = 'interest' . ($i + 1);
    $$var = $interest;
    $i++;
} 

print $interest1 . " - " . $interest2 . " - " . $interest3;

Comments

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.