0

My problem is that I cannot require a page before I start my class code.It doesn't show any errors on finding the page,but it does show some errors that the variable I am trying to access is undefined. I'm trying to access $conn from the class. Here is my code:

<?php

$conn = mysqli_connect("localhost","root","")or die(mysqli_connect_error());
$database = mysqli_select_db($conn,'purchaseproject');
if(!$database){
    echo 'error'.mysqli_error($connect);
}

?>
<?php
require_once './connection.php';
class memberCheck{
    
    static public function isAdmin(){
        $email="foo";
        $sql="SELECT admin FROM members WHERE email='$email'";
        $query=mysqli_query($conn,$sql);
        var_dump($sql);

    }

    static public function usernameAvailable($email){

    }
}

$mc=memberCheck::isAdmin();

?>

I tried changing require_once with require,but it didn't work.So I put it inside the function and it worked fine,yet I'm going to have more than one method and I don't want to copy the same code everywhere.

What do you think about my problem?

  • 1
    Does this answer your question? [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) - The variable `$conn` will be undefined in that method since it's in a different scope from where you defined it. – M. Eriksson Apr 12 '22 at 18:28

0 Answers0