cleibesouza Posted April 5, 2006 Share Posted April 5, 2006 I'm trying to separate the results set of a query into an array. Below is the code:[code]$participationQuery = "SELECT * FROM participation WHERE users_id = '$userID'";$participationResults = mysql_query($participationQuery, $connection) or die (mysql_error());$partNumResults = mysql_num_rows($participationResults)if($partNumResults){while($row = mysql_fetch_array($participationResults)){echo $row['activities_id'];}//end while}//end if[/code]This query gives me the results 2,10,7 without the commas. Meaning that they're all together. I'd need to get them separated. My idea was to dump the results on an array where the final result would be:arrayName[0] = 2arrayName[1] = 10arrayName[2] = 7but can't get that to work. Any ideas/suggestions is greatly appreciated. Thank you all. Quote Link to comment https://forums.phpfreaks.com/topic/6633-from-query-results-to-an-array/ Share on other sites More sharing options...
akitchin Posted April 5, 2006 Share Posted April 5, 2006 to create separation in the data output, you can simply add a separator to the echoing you're doing in the while() loop like so:[code]while (stuff){ echo $row['activities_id'].', ';}[/code]however, you will end up with a residual comma and space at the end. to circumvent this, add a counter to check if it is the first row. echo a comma and space BEFORE all but the first entry:[code]$i = 1;while (stuff){ echo ($i > 1) ? ', '.$row['activities_id'] : $row['activities_id']; ++$i;}[/code]i've used the ternary operator here, but you can expand that out to a full-on if()-else statement if you are more comfortable with that.alternatively, if you want to put the results into an array, you can simply go:[code]while (stuff){ $activity_ids[] = $row['activities_id'];}[/code]at the end of the run, $activity_ids will be an array containing all of the activity IDs found through the query. Quote Link to comment https://forums.phpfreaks.com/topic/6633-from-query-results-to-an-array/#findComment-24086 Share on other sites More sharing options...
cleibesouza Posted April 5, 2006 Author Share Posted April 5, 2006 [!--quoteo(post=361842:date=Apr 5 2006, 12:11 AM:name=akitchin)--][div class=\'quotetop\']QUOTE(akitchin @ Apr 5 2006, 12:11 AM) [snapback]361842[/snapback][/div][div class=\'quotemain\'][!--quotec--]to create separation in the data output, you can simply add a separator to the echoing you're doing in the while() loop like so:[code]while (stuff){ echo $row['activities_id'].', ';}[/code]however, you will end up with a residual comma and space at the end. to circumvent this, add a counter to check if it is the first row. echo a comma and space BEFORE all but the first entry:[code]$i = 1;while (stuff){ echo ($i > 1) ? ', '.$row['activities_id'] : $row['activities_id']; ++$i;}[/code]i've used the ternary operator here, but you can expand that out to a full-on if()-else statement if you are more comfortable with that.alternatively, if you want to put the results into an array, you can simply go:[code]while (stuff){ $activity_ids[] = $row['activities_id'];}[/code]at the end of the run, $activity_ids will be an array containing all of the activity IDs found through the query.[/quote]The array solution worked just fine.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/6633-from-query-results-to-an-array/#findComment-24088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.