CodeMama Posted August 21, 2009 Share Posted August 21, 2009 I am trying to loop through results from my query and store 10 records to an array and then to session: $results = mysql_query($sql) or die(mysql_error()); // Grab results $top10 = $results; $rows = mysql_fetch_array($top10); while ($rows = mysql_fetch_array($top10)){ $rows < 10; $_SESSION['top10'] = $rows; } this is close but not quite right... Link to comment https://forums.phpfreaks.com/topic/171292-storing-sql-results-as-array-to-session/ Share on other sites More sharing options...
gerkintrigg Posted August 21, 2009 Share Posted August 21, 2009 ah... what you're doing here is re-defining the session called "top10" over and over... though I'm unsure why you're doing $rows<10; Take out the first $rows=mysql_fetch_array($top10); I'd just say: $results = mysql_query($sql) or die(mysql_error()); // Grab results $top10 = $results; $i=10; $counter=0; while ($r= mysql_fetch_array($top10)){ if($counter<=$i){ $_SESSION['top10'][$counter]=$r['name']; $counter++; } } I think that this will work. Link to comment https://forums.phpfreaks.com/topic/171292-storing-sql-results-as-array-to-session/#findComment-903340 Share on other sites More sharing options...
play_ Posted August 21, 2009 Share Posted August 21, 2009 $query = "select rating, product_name from products order by rating desc limit 10"; $result = mysql_query($query); $top10 = array(); while( $rows = mysql_fetch_assoc($result) ) { $top10[] = $row['something']; } $_SESSION['top10'] = $top10; Link to comment https://forums.phpfreaks.com/topic/171292-storing-sql-results-as-array-to-session/#findComment-903342 Share on other sites More sharing options...
CodeMama Posted August 21, 2009 Author Share Posted August 21, 2009 one more question about this ... what is the proper syntax to pull the 3 fields while ($r= mysql_fetch_array($bottom10)){ if($counter<=$i){ $_SESSION['bottom10'][$counter]=$r['name']['cviolations']['inDate']; $counter++; } } that causes a fatal error Link to comment https://forums.phpfreaks.com/topic/171292-storing-sql-results-as-array-to-session/#findComment-903451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.