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
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.

Link to comment
Share on other sites


$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.