0

It seems that my $pdo is out of scope for some reason because it returns null but when I move the function code block outside of the function it works.

db.php

<?php

$host = 'localhost';
$db = 'phpcms';
$user = 'root';
$pass = 'root';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE       =>  PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE    =>  PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES      =>  false,  
];

$pdo = new PDO($dsn, $user, $pass, $opt);

functions.php

<?php require_once("include/db.php"); ?>
<?php require_once("include/sessions.php"); ?>
<?php  

    function redirect_to($New_Location)
    {
        header("location:".$New_Location);
        exit();
    }

    function login_Attempt($Username, $Password)
    {
        $Query = $pdo->prepare('SELECT * FROM registration WHERE username=? AND password=?');
        $Query ->execute([$username, $password]);
        if($admin = $Query -> fetch())
        {
            $_SESSION['User_Id']=$admin['id'];
            $_SESSION['Username']=$admin['username'];
            $_SESSION['SuccessMessage']="Welcome Back, {$_SESSION['Username']}";
            redirect_to("dashboard.php");
        }
        else{
            $_SESSION['ErrorMessage']="Incorrect Username and Password Combination!";   
            redirect_to("login.php");           
        }
    }

?>

login.php

<?php require_once("include/db.php"); ?>
<?php require_once("include/sessions.php"); ?>
<?php require_once("include/functions.php"); ?>
<?php  

    if(isset($_POST['Submit'])){

        $username               =   $_POST['Username'];
        $password               =   $_POST['Password'];

        if(empty($username) || empty($password))
        {
            $_SESSION['ErrorMessage']="All fields must be filled out!";
        } 
        else 
        {
            $user = login_Attempt($username, $password);
            //$Query = $pdo->prepare('SELECT * FROM registration WHERE username=? AND password=?');
            //$Query ->execute([$username, $password]);

            //if($admin=$Query -> fetch())
            //{
            //  $_SESSION['User_Id']=$admin['id'];
            //  $_SESSION['Username']=$admin['username'];
            //  $_SESSION['SuccessMessage']="Welcome Back, {$_SESSION['Username']}";
            //  redirect_to("dashboard.php");
            //}
            //else{
            //  $_SESSION['ErrorMessage']="Incorrect Username and Password Combination!";               
            //}
        }
    }
?>

If I remove the commented out code everything works, but when I try to use the login_Attempt function it gives the error "Call to a member function prepare() on null". It doesn't matter where the login_Attempt function is declared, I tried in the functions.php first then I tried moving the method to login.php but still had the same error.

Since the db.php is inserted first, shouldn't the functions.php be inside the pdo's scope?

Madda
  • 21
  • 5
  • I get what a variable scope is.. I don't get why the function is out of the pdo's scope. Should I include the db.php inside the function? – Madda Oct 23 '17 at 06:32
  • Quick hint, pass $pdo into your function as a parameter. – Nigel Ren Oct 23 '17 at 06:47
  • I just included the db.php into the function and it worked. It's probably not the best way to do it. @NigelRen then what would I need to pass in for that parameter? – Madda Oct 23 '17 at 06:50
  • It isn't - it's worth finding out good ways to solve your problems now rather than 'it works'. – Nigel Ren Oct 23 '17 at 06:51
  • Since the db.php is included first, shouldn't I be able to use the $pdo in the function? Why am I getting the error when using a function but not when it's outside the function? – Madda Oct 23 '17 at 06:55
  • The above link to the existing question should give you the answers - that's why it's put there. Read the linked question and it will help. – Nigel Ren Oct 23 '17 at 06:58
  • isn't the $pdo a global variable? – Madda Oct 23 '17 at 07:07
  • Global Variables Are Bad: http://wiki.c2.com/?GlobalVariablesAreBad Maybe this is a little bit better: https://stackoverflow.com/a/45218058/1461181 – odan Oct 23 '17 at 07:22

0 Answers0