Code dies but no error message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Code dies but no error message

Post by me! »

Thus is not working, I can't see any errors in it but line 4 caused it to die. But no error is given? :?:

Code: Select all

 // now look up this users permissions for this group
        $query = "SELECT * FROM permissions WHERE username='$username' AND group_id ='$group_id'";
	$result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result) or die(mysql_error());
        // set the user permissions
        $_SESSION["permission"] = $row['access_levell'];
User avatar
BKiddo
Forum Newbie
Posts: 6
Joined: Tue May 04, 2010 4:00 pm
Location: UK

Re: Code dies but no error message

Post by BKiddo »

Hi me!

A couple of things..

You don't need the or die(mysql_error()); twice - which might be why you're not getting an error.

Code: Select all

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
Should work fine, I think.

Code: Select all

 // set the user permissions
 $_SESSION["permission"] = $row['access_levell'];
^the code above could also be causing problems. I don't think you can set a global variable like that, what are you trying to do here?
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Code dies but no error message

Post by me! »

Removed the second or die and the code runs til the end, but does not do what I want it to.

This is what I am looking for:

Code: Select all

// now look up this users permissions for this group
// get the users permission level from the permissions table where the user name = X and the group_id = Y
$query = "SELECT * FROM permissions WHERE username='$username' AND group_id ='$group_id'";

// get it...	
$result = mysql_query($query) or die(mysql_error());

// get the info from the row
$row = mysql_fetch_array($result);

// take what in in the DB for access_level and register it to  $_SESSION["permission"]  for future use
        $_SESSION["permission"] = $row['access_levell'];
User avatar
BKiddo
Forum Newbie
Posts: 6
Joined: Tue May 04, 2010 4:00 pm
Location: UK

Re: Code dies but no error message

Post by BKiddo »

I'd do a quick test then to make sure that the row is definitely being pulled from the database.

Do this by print_r($row); if you get the array printed out then you know the mysql is working fine.

If the mysql is working then how about checking these things:

1) Is the row column in the pemissions table definitely called access_levell? I'm asking as it looks like it has 2 L's or is that a number 1?
2) $_SESSION["permission"] <-- you're using " there instead of '. Not sure if that makes a difference though.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Code dies but no error message

Post by me! »

I thought you found it with the two L's, that was an error but not all of it.

I changed the " to ' But I don't thing that does anaything in this case since others are working.

I print_r($row); and get nothing...
Checked, table name and column name they are good...
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Code dies but no error message

Post by me! »

FOUND IT!
When you are asking form data in the db that matches $username and $group_id you want to make sure there is a user with the proper group_id !!!!!!!!

I just spent the last 1.5 hours pulling my hair out on this! :drunk:
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Code dies but no error message

Post by me! »

This was the final result, Thanks

Code: Select all

 // now look up this users permissions for this group
        $query = "SELECT * FROM permissions WHERE username='$username' AND group_id ='$group_id'";
	$result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result);
            if(mysql_num_rows($result)!='0'){ // If match.
            
            $_SESSION['permission'] = $row['access_level'];// set the user permissions
            $_SESSION['username'] = "$username"; // Craete session username.
            $_SESSION['group_id'] = "$group_id"; // set the group_id
                
            }else{
            $message.="<div class=\"errorbox\">You do not have any access rights to this group</div>";
            }
User avatar
BKiddo
Forum Newbie
Posts: 6
Joined: Tue May 04, 2010 4:00 pm
Location: UK

Re: Code dies but no error message

Post by BKiddo »

Glad you got it sorted!
Post Reply