Explode array and define Variables
I have the following which returns
"soccertennisfootball"
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "{$row['interest']}";
}
Can somebody please explain how I can explode the data and assign it as variables? I've been looking around at tutorials but they all seem to have some sort of delimeter?
Ive tried the following oly its acting very weird and printing out multiple times?
the following...
$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[0];
$interest2 = $interests[1];
$interest3 = $interests[2];
print $interest1 . " - " . $interest2 . " - " . $interest3;
}
Prints out
"Tennis - Tennis - Footy - Tennis -Soccer - Footy"
They aren't a single string, you're just echoing it out. Store it somewhere. This code stores it in a new array. It may be useful but it really depends on what you want to do with it.
$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$interests[] = $row['interest'];
}
In each iteration of the while loop, you can only access one of the results (first 'soccer', then 'tennis', then 'football'). In your second code block, you're trying to somehow access all three inside the while loop.
Instead, inside the while loop, push that single result into an array:
$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$interests[] = $row['interest'];
//First 'soccer' will be pushed into $interests, then 'tennis', then 'football'.
}
Now that you have all the values in the array, you can access each individually. This is done OUTSIDE the while loop:
echo $interests[0] . ' - ' . $interests[1] . ' - ' . $interests[2];
This will print "soccer - tennis - football".
Alternately, like some have said, you can loop through the $interests array and echo each value, or do whatever else you'd like with them.
Put them in an array, then do as you will with it after.
$interests = array();
while (...)
{
$interests[] = $row['interest'];
}
Use extract method from php http://php.net/manual/en/function.extract.php
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
echo $interest;
}
Use below snippet to store values in array
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$result = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$result[] = $row['interest']
}
print_r($result);
Given your clarifications (using MySQL, interest
is the only column, and sample output) I would suggest using GROUP_CONCAT()
. This puts it all on the MySQL side and allows you to pull the single row from $result
.
Try the following sample query:
$query = "SELECT GROUP_CONCAT(interest SEPARATOR ' - ') FROM user_interests
WHERE user_id = " . $usersClass->userID() . "
GROUP BY user_id";
精彩评论