Jump to content

Classes Problem


Drezard

Recommended Posts

I have 2 files in use.

One is index.php, the other is classes.php. Index.php contains all of the website layout and executes the code that is contained in a class (myclass), the class(myclass) is defined in classes.php. Now i have this line of code at the top of index.php:

[CODE]
include('classes.php');

error_reporting(E_ALL);
[/CODE]

I then have a function in the class(myclass) called check_priv(); Basically all it does is checks the database and the table users for the priv of certain users. Another function that is already executed (and works) connects and selects the database. Now here is the exact code for function check_priv()...

[CODE]
function check_priv() {

if (isset($_SESSION['user']) && isset($_SESSION['pass'])) {

$user = $_SESSION['user'];
$pass = $_SESSION['pass'];

$query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";

$priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

}
}
[/CODE]

But, whenever i try and use $priv in index.php (like if ($priv == 1)) it gives me this error...

[QUOTE]
Notice: Undefined variable: priv in /home/wintersw/public_html/developer/index.php on line 77
[/QUOTE]

I have tried adding a public variable to the class called $priv, then definining it using that method...

Whats wrong and how do i fix it?

- Cheers, Daniel
Link to comment
Share on other sites

first of all, did you make a new object variable for your class?

2nd of all, you have to actually return $priv inside your function.

example:
[code]
<?php
  class blah {
      function check_priv() {
        if (isset($_SESSION['user']) && isset($_SESSION['pass'])) {
            $user = $_SESSION['user'];
            $pass = $_SESSION['pass'];
            $query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";
            $priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
            return $priv;
        } // end if
      } // end function check_priv
  } // end class blah

  $blarg = new blah;
 
  $result = $blarg->check_priv();

  if ($result) {
      // do something with result
  } else {
      // user not authorized
  }
?>
[/code]

that's just based off your current code tho. you should probably put pretty much all of that inside your class in the first place.
Link to comment
Share on other sites

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.