-1

My Errors Are:

Notice: Undefined variable: sql in C:\xampp\htdocs\framework\frame.php on line 20

Notice: Undefined variable: sql in C:\xampp\htdocs\framework\frame.php on line 20

Notice: Undefined variable: sql in C:\xampp\htdocs\framework\frame.php on line 20

Notice: Undefined variable: sql in C:\xampp\htdocs\framework\frame.php on line 20

Notice: Undefined variable: dbC in C:\xampp\htdocs\login.php on line 17

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\login.php on line 17

I have a file that includes another (PHP)

config.php:

$sql_host = "localhost";
$sql_user = "root";
$sql_pass = "";
$sql_databasename = "data";

Simple, right?

Now to frame.php:

    require($_SERVER['DOCUMENT_ROOT'] . "/inc/config.php");
$sql = array(
        "host" => $sql_host,
        "user" => $sql_user,
        "pass" => $sql_pass,
        "dbid" => $sql_databasename,
    );

    foreach ($sql as $v){
        if (!isset($v)){
            die("ERROR: SQL LOGIN INVALID");
        }
    }

    function connectSQL(){
        $dbC = new mysqli($sql["host"], $sql["user"], $sql["pass"], $sql["dbid"]);

        if ($dbC->connect_error){
            die("SQL Error: " . $dbC->connection_error);
        }
    }

And I don't understand why this is happening? Any ideas?

iceybeta
  • 33
  • 1
  • 1
  • 3

1 Answers1

0

It's a variable scope problem, $sql is not in scope in the connectSQL function. I suggest passing it as an argument:

function connectSQL($sql){
    ...
}

Call it:

connectSQL($sql);
MrCode
  • 63,975
  • 10
  • 90
  • 112