Select into Session

Discussion in 'PHP' started by piropeator, Aug 5, 2010.

  1. #1
    Hello.
    Using PHP. How do I put the result of a select into a session and after that read that records for to show in a display??
     
    piropeator, Aug 5, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    It depends on how you'd put it in a session variable. It should be something like this:
    
    while ($row = mysqli_fetch_array($result)) $watever[] = $row['lol'];
    $_SESSION['poop'] = $watever;
    
    for($i=0; $i < count($_SESSION['poop']); $i++) echo $_SESSION['poop'][$i];
    
    PHP:
    Also, see this. :)
     
    Rainulf, Aug 5, 2010 IP
  3. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    
    $q = mysql_query("SELECT id FROM blah");
    $i = 0;
    while ($res = mysql_fetch_array($q)) {
     $_session[$i]['id'] = $res['id'];
    $i++;
    }
    
    PHP:
    assumes session already set
     
    themullet, Aug 5, 2010 IP
  4. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #4
    That works if I want several fields??
    select id, code, name, date, zip, address from persons
    PHP:
     
    piropeator, Aug 5, 2010 IP
  5. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Duh not. You never mentioned a field in your first post..

    Here:
    
    while ($row = mysqli_fetch_array($result)) 
    $watever[] = array ( 'id'      => $row['id'], 
                         'code'    => $row['code'], 
                         'name'    => $row['name'], 
                         'date'    => $row['date'], 
                         'address' => $row['address'] 
                      );
    
    $_SESSION['poop'] = $watever; // this is the general idea!
    
    foreach ($_SESSION['poop'] as $yeaa) echo $yeaa['id'] . $yeaa['code'] . $yeaa['name'] . $yeaa['date'] . $yeaa['address'];
    
    PHP:
     
    Last edited: Aug 5, 2010
    Rainulf, Aug 5, 2010 IP
  6. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #6

    Do you have any idea, how PHP serializes these arrays to put into sessions (which can physically contain nothing but string)?
     
    techbongo, Aug 5, 2010 IP
  7. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #7
    Not sure, but it should theoretically create a two-dimensional associative array if you do this:
    $_SESSION['poop'] = $watever;
    PHP:
    If so, then this should be possible:
    
    <?php
    session_start( );
    $watever['meow'] = "poop";
    $_SESSION['lol'] = $watever;
    echo $_SESSION['lol']['meow']; // something like this
    ?>
    
    PHP:
    I haven't tested it myself though, but it should be theoretically possible. :)
     
    Rainulf, Aug 5, 2010 IP
  8. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    those variable names made me chuckle.

    As the mysql_fetch_array is already an array, this also works
    
    session_start();
    while ($res = mysql_fetch_array($qid)) {
    	$_SESSION['timmah'] = $res;
    }
    echo $_SESSION['timmah']['id'];
    
    PHP:
     
    themullet, Aug 5, 2010 IP