Jump to content

referencing class variables in queries


rubing

Recommended Posts

I am writing a user manager class.  I want to have multiple levels of users and so am declaring a variable $user_level.  Can I refer to $user_level in sql queries as follows?

 

class UserManager
{

  $user_level 

  public function createAccount
  (    
    $in_email,
    $in_pw,
    $in_year,
    $in_month,
    $in_day,
  )
  {
// bunch of validation and other code....

  //QUERY TO INSERT USER
     $qstr = <<<QUERY
INSERT INTO Users
      (userlvl,password,full_name,user_email,birthdate)
     VALUES ('$this->user_level', '$pw','$email',
             '$in_year-$in_month-$in_day')
QUERY;

Link to comment
https://forums.phpfreaks.com/topic/100660-referencing-class-variables-in-queries/
Share on other sites

Like all other variables, if you are using it outside of the local scope of the class/function, you can it by calling the global value.

global $user_level

It's not clear from your code where it falls.

 

If it is within it, you still can reference the object.

 

If it's afterwards:

$class = "UserManager";

$object = new $class;

echo "{$object->user_level}\n"; //you can call it like that.

 

And if you are using throughout, you may want to specify if the variable will be public or private - probably private.

 

 

 

 

Archived

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

×
×
  • 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.