Gurboura Posted November 11, 2008 Share Posted November 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/ Share on other sites More sharing options...
trq Posted November 11, 2008 Share Posted November 11, 2008 Is this your exact code? Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688079 Share on other sites More sharing options...
Gurboura Posted November 11, 2008 Author Share Posted November 11, 2008 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688082 Share on other sites More sharing options...
trq Posted November 11, 2008 Share Posted November 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688085 Share on other sites More sharing options...
Gurboura Posted November 11, 2008 Author Share Posted November 11, 2008 Any idea on what I can do to do this? Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688086 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688163 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 No one has idea on how I can do this? All I want is it to check the user level of the user and display links according to that user level Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688166 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688168 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688173 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688174 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688175 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688179 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 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 What? Store a users access level within there $_SESSION array when they login? Thats what my other example show you. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688180 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 I used it and all I got a white blank page when I posted it in my PHP file that runs all the login stuff Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688181 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 Its an EXAMPLE. You need to modify it to fit your database and form. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688182 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 I know that, you seem to be doubting my common sense I didn't copy and paste it and not change everything and expect it to work Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688184 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688186 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 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> ] '; } } Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688191 Share on other sites More sharing options...
Gurboura Posted November 12, 2008 Author Share Posted November 12, 2008 Problem solved. Thanks premiso and thrope! Quote Link to comment https://forums.phpfreaks.com/topic/132345-solved-session-issue-getting-mysql-string-displayed/#findComment-688215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.