jas4 Posted August 29, 2007 Share Posted August 29, 2007 Hi, trying to get my head around arrays, and I want to take all the results from a query and store them in an array so that I can use them for later use: here's what i've got so far: $selectDistinct = mysql_query("SELECT DISTINCT mid FROM temp WHERE rand = '$rand'") or die (mysql_error()); $array = array(); while ($row= mysql_fetch_assoc($selectDistinct)){ $mid = $row['mid']."<br>"; $array($mid); <<-----this is the line im certain is not correct } and to display the array for later use: foreach ($array as $value) { echo $value; } thanks Quote Link to comment https://forums.phpfreaks.com/topic/67211-php-array-help/ Share on other sites More sharing options...
Ken2k7 Posted August 29, 2007 Share Posted August 29, 2007 You should do this: <?php $array = array(); $count = 0; $selectDistinct = mysql_query("SELECT DISTINCT mid FROM temp WHERE rand = '$rand'") or die (mysql_error()); while ($row= mysql_fetch_assoc($selectDistinct)){ $mid = $row['mid']."<br>"; $array($count) = $mid; $count++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67211-php-array-help/#findComment-337095 Share on other sites More sharing options...
hostfreak Posted August 29, 2007 Share Posted August 29, 2007 Your both using the wrong brackets. It should be $array[] = value; Take a look at: http://www.php.net/types.array Edit: Although, I don't understand why you would do that when you can do it with mysql_fetch_assoc or mysql_fetch_array when needed Quote Link to comment https://forums.phpfreaks.com/topic/67211-php-array-help/#findComment-337105 Share on other sites More sharing options...
Ken2k7 Posted August 29, 2007 Share Posted August 29, 2007 Oops, nice catch hostfreak. <?php $array = array(); $count = 0; $selectDistinct = mysql_query("SELECT DISTINCT mid FROM temp WHERE rand = '$rand'") or die (mysql_error()); while ($row= mysql_fetch_assoc($selectDistinct)){ $mid = $row['mid']."<br>"; $array[$count] = $mid; $count++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67211-php-array-help/#findComment-337108 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.