2

So I have a simple working user log in script that carries forward myusername and mypassword.

What I am trying (and failing) to do is recall a specific piece of information from that users Mysql database to modify the page that they are on.

MYSQL DATABASE ('members'): 
id:1
username:cornellmatt
password:encrypted(md5)
email:[email protected]
active: 0/1
**permission:1**

The field I need to extract is permission, as it holds the number that will relate the the scene that the user is currently on. In order to do this I am running Jquery on the page that you arrive at after logging in, that links to connect.php

<script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
<script type="text/javascript">

    $.get("connect.php", function(data) {
    alert("Data Loaded: " + data);
        });

I understand that there are all kinds of problems with my JavaScript at the moment, but for now I want to get the server side stuff done so I can focus on the JavaScript fully The (Current) problem with my Php is that it doesn't seem to be getting anything from the database at all, I know it is connected, it just wont echo the value inside 'permission';

connect.php:

<?php
include "Session.php"; 

$dbhost = "host";
$dbuser = "username";
$dbpass = "password";
$dbname = "databasename";
    //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($dbname) or die(mysql_error());
    // Retrieve data from Query String


//this selects everything for the current user, ready to be used in the script below

$result = mysql_query("SELECT * FROM members WHERE username = '$myusername' LIMIT 1");

//this function will take the above query and create an array
while($row = mysql_fetch_array($result))
  {

 //with the array created above, I can create variables (left) with the outputted array (right)
$permission = $row['permission'];
  } 


        echo '<u><b>Permision ID:</b></u> - - No:' . $row['permission']  ;
?>

If you are wondering what'Session.php' is, it is a script that handles the startup, and is the reason I know I am still logged in. Courtesy of Daryl Gill who helped me in a previous post.

Session.php

 <?php
session_start();

if (!isset($_SESSION['Username']))
{
 include "Logout.php"; 
 exit;
// session is not set, so enforce logout
}
?>

So my quest is, Why doesn't connect.php echo the value permission, even when I go to the page direct? Please help, I am currently coding this for my student project and have been going in circles for days.

3
  • 4
    I've removed the database credentials from your question. If these credentials were valid you may want to delete this question, change your password and re-ask it as they will still appear in the edit history. Commented Mar 22, 2013 at 11:46
  • 1
    Also change your password on every other service where that password is used. Commented Mar 22, 2013 at 11:48
  • Thanks, sorry I was in a bit of a state when I put this up, my mind was fried, thanks for editing it :) Commented Mar 22, 2013 at 12:04

3 Answers 3

1

When $_SESSION['Username'] is not set you have putted exit;

And because of that it doesn't prints permission

And When you do Ajax call may be the $_SESSION['Username'] is set and it prints permission.

Also change

echo '<u><b>Permision ID:</b></u> - - No:' . $row['permission']  ;

to

echo '<u><b>Permision ID:</b></u> - - No:' . $permission;

Because outside while loop you can't access $row['permission'].

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I changed it to the php variable now, and I understand why it didn't work, thank you so much for the feedback
@MattCornell Glad it helped.. Accept the appropriate answer which has helped you..
1

$row['permission'] doesn't exist here. You just need $permission.

echo '<u><b>Permision ID:</b></u> - - No:' . $permission;

2 Comments

I entered it in that format and now it works, thank you so much
Mark as answered please :)
0

Make sure you get any rows by doing this:

while($row = mysql_fetch_array($result)) { var_dump($row); $permission = $row['permission']; }

1 Comment

Thanks, now I can see exactly what's happening

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.