Jump to content

Archived

This topic is now archived and is closed to further replies.

Drezard

Classes Problem

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.