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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Session variable not echoing

    Ok well in dologin.php (~process.php), I sessioned. now in acccount.php i cannot echo them:


    doLogin.php
    PHP Code:
    <?php 
    session_start
    ();
    include(
    "dbsettings.php");  
    mysql_connect("$host""$username""$password")or die("cannot connect");   
    mysql_select_db("$db_name")or die("cannot select DB");  
       
    $username mysql_real_escape_string($_POST['username']);  
    $password md5(mysql_real_escape_string($_POST['password']));  

    $sql="SELECT * FROM `user` WHERE `username`='{$username}' AND `password`='{$password}'";  
    $result=mysql_query($sql);  
       
    // do the check  
    if($result)  
    {  
        if(
    mysql_num_rows($result) == 1)  
        {  
            
    $_SESSION['username'] = $username;
            
    $_SESSION['password'] = $password;
            
    $_SESSION['firstname'] = $firstname;
            
    $_SESSION['lastname'] = $lastname;
            
    $_SESSION['account'] = $account;
               
            
    header("location: account.php");  
                exit();  
        }  
        else  
        {  
          echo 
    "Wrong username/password.";  
        }  
    }  
    else  
    {  
        echo 
    "The query is not true.";  
    }  
    ?>
    account.php
    PHP Code:
    <?php session_start(); ?>  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>Account Dashboard</title>  
    </head>  
    <body>  
    <?php  
    include('dbsettings.php');  

    $con mysql_connect("$host","$user","$password");  
    if (!
    $con)  
      {  
      die(
    'Could not connect: ' mysql_error());  
      }  

    mysql_select_db("$db_name"$con);  
    $username$_SESSION['username']; // Editted 

    $sql="SELECT * FROM `user` WHERE `username`='{$username}'";  

    $result mysql_query("$sql");  

    while(
    $row mysql_fetch_array($result))  
      {
      echo 
    "Welcome, ";  
     
    // echo $row['firstname'] . " " . $row['lastname'];  
      
    echo"$username";
        echo 
    "<br />";
      echo
    "$firstname";
        echo 
    "<br />";
      echo
    "$lastname";
        echo 
    "<br />";
      echo
    "$account";
        echo 
    "<br />";
      echo
    "$username";
        echo 
    "<br />";
      
      }  
      
    mysql_close($con);  
    ?>  
    <a href="logout.php"> Log Out </a>  
    </body>  
    </html>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    firstly, this is a problem with your php, not your sql.

    Based on your previous questions, you're trying to put code together that you don't understand. You'll do better to learn what each bit of your code is doing.

    In regards to your current problem, your echo'ing variables that don't exist in your while loop. Your row information is stored in the row array - to access specific row information, you have to use the row array like the line you commented out in the while loop (example: $row['field']).


  •  

    Tags for this Thread

    Posting Permissions

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