Jump to content

[SOLVED] Session Issue - Getting MySQL String displayed


Recommended Posts

I'm trying to pull data from MySQL, it's a numeric number for access levels, and store it in the session. I'm still new to this, but I'm starting to get ahang of PHP, Sessions and MySQL, but this just keeps giving me issues.

Here's the code I'm using to pull the data:

$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

  }  

 

So $level is the number I'm looking for. I have this for the session:

session_start();
$_SESSION["logged"] = 1;
$_SESSION['username'] = $firstname;  
$_SESSION['level'] = $level
header("location:../index.php");
}
else {
$_SESSION["logged"] = 0;
header("location:../index.php");

}

 

Whenever I try to call it with:

Your level is {$_SESSION['level']}

 

I get this displayed:

Level is SELECT access_level FROM users WHERE username='*****' and password='****' and access_level=access_level

Of course username is blocked out for security reasons.

 

Am I missing something? Doing something completely wrong?

This is the full exact code.

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");
$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

}  

$result=mysql_query($sql);

$count=mysql_num_rows($sql);

if($count==1){

session_start();
$_SESSION["logged"] = 1;
$_SESSION['username'] = $firstname;  
$_SESSION['level'] = $level
header("location:../index.php");
}
else {
$_SESSION["logged"] = 0;
header("location:../index.php");

}

Not possible with that code I'm afraid.

 

ps: This line....

 

$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

 

needs to go. So does your while loop if your only expecting 1 record.

Its pretty hard to tell what your trying to do actually. Your second query selects 'access_level', but your using 'access_level' in your WHERE clause. This would indicate that already know its value, so why are you querying the database for it?

 

Based on what I think your trying to do I'll post a simple example.

 

<?php

if (isset($_POST['username']) && isset($_POST['userpass'])) {
  $username = mysql_real_escape_string($_POST['username']);
  $userpass = mysql_real_escape_string($_POST['userpass']);

  $sql = "SELECT access_level FROM $tbl WHERE username = '$username' && userpass = MD5('$userpass')";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      session_start();
      $_SESSION['logged'] = 1;
      $_SESSION['username'] = $username;  
      $_SESSION['level'] = $row['access_level'];
      header("location: http://yoursite.com/index.php");
    } else {
      header("location: http://yoursite.com/index.php");
    }
  }
}

?>

 

Then, in index.php

<?php

session_start();
if (isset($_SESSION['logged'])) {
  echo "Hello {$_SESSION['username']}!";
} else {
  echo "You are not logged in";
}

?>

 

Thats the basics of a login script.

I have the login script and all that setup, I'm just trying to pull the access_level, and display links according to that.

 

Say if the access_level is 1, it'll display links to forums, articles, downloads

If it's 2, it'll display forums, add article, manage article, articles, downloads

If it's 3, it'll display admin area, forums, add article, manage, articles, downloads

Using thorpe's script:

 

<?php

session_start();
if (isset($_SESSION['logged'])) {
  echo "Hello {$_SESSION['username']}!";
} else {
  die("You are not logged in");
}

if ($_SESSION['level'] == 1) {
    $link = "Forums, Articles Downloads";
}elseif ($_SESSION['level'] == 2) {
    $link = "Forums, Manage, Add Article, Add Downloads";
}

echo $link;
?>

 

I think thats what you are looking for?

Well, then. Are you storing a users access level within there $_SESSION array when they log in? That would make it real easy.

 

<?php

  session_start();
  if (isset($_SESSION['logged'])) {
    switch ($_SESSION['level']) {
      case 1:
        // links for level 1
        break;
      case 2:
        // links for level 2
        break;
      case 3:
        // links for level 3
        break;
    }
  } else {
    // not logged in.
  }

?>

 

No point querying the database when the data you want should already be in a users session.

     

Well, then. Are you storing a users access level within there $_SESSION array when they log in? That would make it real easy.

That's what I was trying to do :P

 

What? Store a users access level within there $_SESSION array when they login?

 

Thats what my other example show you.

This is the full exact code.

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");
$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

}  

$result=mysql_query($sql);

$count=mysql_num_rows($sql);

if($count==1){

session_start();
$_SESSION["logged"] = 1;
$_SESSION['username'] = $firstname;  
$_SESSION['level'] = $level
header("location:../index.php");
}
else {
$_SESSION["logged"] = 0;
header("location:../index.php");

}

 

Anyhow given that. I am trying to see how this works:

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");
$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

} 

 

You have 2 sql statements, and yet you are trying to fetch first_name from a query that only selects access_level. Why even have the second query?

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");

while($row = mysql_fetch_array($sql))
  {

$level = $row['access_level'];
echo $level . " - Debug see what is coming out of the DB. ";
$firstname = $row['first_name'];

} 

 

I would try that and see what is coming out of the database.

 

My hunch is that you are somehow updating the table and setting the access level to a string in some part of the code we cannot see?

 

If $level is echoed as the SQL statement than that is exactly your problem. I would look at where the data gets inserted into the DB for the user.

 

Let us know what happens.

This is the full exact code.

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");
$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

}  

$result=mysql_query($sql);

$count=mysql_num_rows($sql);

if($count==1){

session_start();
$_SESSION["logged"] = 1;
$_SESSION['username'] = $firstname;  
$_SESSION['level'] = $level
header("location:../index.php");
}
else {
$_SESSION["logged"] = 0;
header("location:../index.php");

}

 

Anyhow given that. I am trying to see how this works:

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");
$levelpull = mysql_query('SELECT access_level FROM $tbl WHERE username="$username" and access_level=access_level');

while($row = mysql_fetch_array($levelpull))
  {

$level = $row['access_level'];
$firstname = $row['first_name'];

} 

 

You have 2 sql statements, and yet you are trying to fetch first_name from a query that only selects access_level. Why even have the second query?

 

$sql = mysql_query("SELECT * FROM $tbl WHERE username='$username' and password='$password'");

while($row = mysql_fetch_array($sql))
  {

$level = $row['access_level'];
echo $level . " - Debug see what is coming out of the DB. ";
$firstname = $row['first_name'];

} 

 

I would try that and see what is coming out of the database.

 

My hunch is that you are somehow updating the table and setting the access level to a string in some part of the code we cannot see?

 

If $level is echoed as the SQL statement than that is exactly your problem. I would look at where the data gets inserted into the DB for the user.

 

Let us know what happens.

 

thrope's code fixed it. I forgot I had the level session defined in the index.php file, but I liked thorpe's code better than what I had.

Now I need to figure out how to display display links according to level. I tried using your premiso, and it didn't work. I have the messages coming out of an echo.

 

if(isset($_SESSION['username'])) 
{
  
  if(@isset($_GET['logout'])) 
  {
    
    unset($_SESSION['username']);
    echo "You have been logged out <a href='?' class='topR'>Continue</a>";
  }
  
  {
      
      echo "Welcome {$_SESSION['first_name']}!  <input name='logout' class='logoutSubmit' type='button' value='Logout' onClick=\"parent.location='?logout=true'\">";
  }
}
else
{
  
  if(isset($_POST['username'])) 
  {
    echo "You have been logged in! <a href='?' class='topR'>Continue</a>";
  }
  else 
  {
   
    echo '
	    Username: 
            <input type="text" name="username" class="loginUser">
	   | Password: 
            <input type="password" name="password" class="loginPass">
		<input type="submit" name="login" class="loginSubmit" value="Login" style="font:Verdana;font-size:11px;">
		  |  
		Not registered? [ <a href="/signup.php" class="topR">Register now!</a> ]
	';
  }
} 

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.