Jump to content

storing sql results as array to session?


CodeMama

Recommended Posts

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;
     }

 

:confused: 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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.