Function error when used on another page

Asked

Viewed 39 times

-2

I have the following code

class Auth {
    function getMemberByUsername($username) {
        $db_handle = new DBController();
        $query = "Select * from members where member_name = ?";
        $result = $db_handle->runQuery($query, 's', array($username));
        return $result;
    }
}

at index I call $user = $auth->getMemberByUsername($username); and I can use the user variable normally. In the same class I also have the following fuction:

function getMemberNivel($level) {
    $db_handle = new DBController();
    $query = "Select * from members where nivel = ?";
    $result = $db_handle->runQuery($query, 'i', array($level));
    return $result;
}

and I call as follows:

$phase = $auth->getMemberNivel($level);

but informs me that the variable $level there is no, I do not understand what happens, if anyone can help me thank you, because I have spent hours breaking my head with something that may be very simple.

  • Wow, it was all organized, it was time to publish messed up everything

  • The OS uses markdown, in the question text field there is an explanation of how to use the main. And where this variable came from $level?

  • I’m kind of a beginner yet, don’t I get it, what do you mean by so and markdown?? and the variable $level is declared inside the function, as well as the $username.

  • I’m researching markdown

  • OS = Stackoverflow (site of questions and answers that we are using). Markdown = markup language (https://answall.com/help/formatting). Which line is the error?

  • NESTA: $Phase = $auth->getMemberNivel($level); it does not recognize the level variable, but it is there in the function.... isn’t it so ? I followed the example of the others, because in the same file you have other functions, I just copied the function getMemberByUsername and changed the variable name and select

  • Your error is programming logic, when you call a function by passing arguments, these must exist, for example, $level = 5; $phase = $auth->getMemberNivel($level); if you do not declare the variable before passing as argument of a function will give error, because it is passing something that does not exist (was not declared)

Show 2 more comments

2 answers

0

Try to make the following change in both functions.

function getMemberByUsername($username='') {
   $db_handle = new DBController();
   $query = "Select * from members where member_name = ?";
   $result = $db_handle->runQuery($query, 's', array($username));
   return $result;
}

function getMemberNivel($level='') {
   $db_handle = new DBController();
   $query = "Select * from members where nivel = ?";
   $result = $db_handle->runQuery($query, 'i', array($level));
   return $result;
}

This will prevent the error from happening again, and do not forget to treat the return of variables. Ex.:

if(empty($username)){
   return "Username não pode ser vazio"
}
  • the strange thing is that getMemberByUsername does not present problems, and other functions that are in the same file not, and even not having variables previously declared, only within the same function, and this solution that you suggested I had already tried, I’ll do it again, but I’ll do it another way. But if you have any more suggestions you can send.

  • I don’t know what your code looks like, but I imagine the $level variable isn’t getting to the function.

  • oops, it did, but it came empty, how do I get it to arrive with some value as well as the others? the select is not for this ? , in the case of the username it searches in the fields filled by the user, based on the login and password have to bring me tbm at the level of access of the user ($level)

  • It’s because you can’t know exactly how you are sending the data, in your doubt you only put the demonstration of functions, this solution I posted is never to make a mistake. I need more data to help you.

  • In vdd this code works perfectly, what I need is a login screen that stores the user option always stay logged in. What I need is for access level conditions, in case user or admin, so I created Function to search (through select) the access level of the guy who is trying to access. and in the index there is an if block where I put the condition that should be in the variable $level, if 1 is admin if 2 user. I’ll try to put the code here to see if you can help me, I’ve been stuck in it since yesterday.

  • Okay, I’m on hold.

  • I tried to post the prints but blocked affff

  • I think I solved it young, more tests will be necessary ...

Show 3 more comments

-1

runQuery($query, ’s', array($username)); Return $result; } Function getTokenByUsername($username,$expired) { $db_handle = new Dbcontroller(); $query = "Select * from tbl_token_auth Where username = ? and is_expired = ?" ; $result = $db_handle->runQuery($query, 'si', array($username, $expired)); Return $result; } Function markAsExpired($tokenId) { $db_handle = new Dbcontroller(); $query = "UPDATE tbl_token_auth SET is_expired = ? WHERE id = ?" ; $expired = 1; $result = $db_handle->update($query, 'ii', array($expired, $tokenId)); Return $result; } Function insertToken($username, $random_password_hash, $random_selector_hash, $expiry_date) { $db_handle = new Dbcontroller(); $query = "INSERT INTO tbl_token_auth (username, password_hash, selector_hash, expiry_date) values (?, ?, ?)"; $result = $db_handle->Insert($query, 'ssss', array($username, $random_password_hash, $random_selector_hash, $expiry_date)); Return $result; } Function getMemberNivel($level='2') { $db_handle = new Dbcontroller(); $query = "Select * from Members Where nivel = ?" ; $result = $db_handle->runQuery($query, 'i', array($level)); Return $result; } Function update($query) { mysqli_query($this->Conn,$query); } } ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.