Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    USB
    USB is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Store MySQL row in array

    Hi, i'm working on a login script. My problem is that I want to grab all the data that the sql table is storing for that user. Example my table is holding the values "id, username, passsword".

    What i've done so far is setting a session $_SESSION['username'] = $username;
    and what i want to do now is also set a session for the id so I can use that later. $_SESSION['id']. Something like this.

    so is there anyway to store the whole table row in an array and easily grab the value I want when needed? like result['id'] for the id and result['username'] for the username and so on...

    PHP Code:
    <?php

    $mysqli 
    = new mysqli("localhost""root""password""ls");

    if (
    mysqli_connect_errno()) 
    {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    if(
    $stmt $mysqli -> prepare("SELECT * FROM tAccounts WHERE username = ? AND password = ?"))
    {
        
    $stmt -> bind_param('ss'$username$password);
        
        
    $username $_POST['username'];
        
    $password $_POST['password'];
        
        
    $stmt -> execute();
        
        
    $stmt -> bind_result($username$password);
            
        if(
    $stmt -> fetch())
        {
            
    session_start();
            
    $_SESSION['username'] =  $username;
            
    header('Location: account.php');
        }
        else
        {
            
    header('Location: index.php');
        }
    }

    function 
    LogOut()
    {
        
    session_start();
        
    session_destroy();
        
    header('Location: index.php');    
    }

    if(
    function_exists($_GET['f']))
    {
        
    $_GET['f']();    
    }

    ?>
    what i'm trying to say is that I want to grab the data something like this:

    PHP Code:
        session_start();
        
    $_SESSION['username'] =  result['username'];
        
    $_SESSION['id'] = result['id'];
        
    header('Location: account.php'); 
    Last edited by USB; 05-26-2013 at 03:30 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    PHP Code:
    foreach($Row as $Key => $Value)
       {
       
    $_SESSION[$Key] = $Value;
       } 
    Jpb done
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    To do an actual row into a session with statements isn't quite that easy though.
    You could fetch the result, and then fetch a row:
    PHP Code:
    $stmt->execute();
    $result $stmt->get_result();
    $_SESSION['row'] = $result->fetch_assoc(); // this is multidimensional.  Can be merged as well 
    Or if you don't actually need the row and prefer the offsets, you can do what you are already doing:
    PHP Code:
    $stmt->execute();
    $stmt->bind_result($field1$field2$field3);
    if (
    $stmt->fetch())
    {
        
    $_SESSION['field1'] = $field1;
        
    $_SESSION['field2'] = $field2;
        
    $_SESSION['field3'] = $field3;

    You could also reference a variable that's represented within the bind_result to that of a SESSION offset.
    Keep in mind that the bind_result order must match that of your query. Since you've use * as your query fields, I haven't a clue what fields are in there. All I know of for sure is username and password since it would have tossed an exception on prepare if they didn't exist.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •