0
function connectMySQL()
{
    $link = mysqli_connect("localhost", "", "", "");

    global $link;
    return $link;
}

function checkCredentials($username, $password)
{
    connectMySQL();

    print_r($link);
}

When using checkCredentials function, I get the following error:

Notice: Undefined variable: link in C:\xampp\htdocs\zone\funcs.php

If I add the following code to the connectMySQL function, all the correct information is shown

print_r($link);
exit();

It's just when the function is called in the checkCredentials function that it says it is undefined. I don't believe this happened before I started switching code to MySQLi

Joe Torran
  • 13
  • 1
  • The `$link` variable doesn't exist inside the `checkCredentials` function. Changing the line inside your function to `$link = connectMySQL();` will fix the issue. Read about [variable scope](http://www.php.net/manual/en/language.variables.scope.php). – Amal Murali Jan 22 '14 at 15:30

4 Answers4

1

The global keyword doesn't put any variables into global scope, it imports them from there, your functions rather should look like this.

function connectMySQL() {
    return mysqli_connect("localhost", "", "", "");
}

function checkCredentials( $username, $password ) {
    $link = connectMySQL();
    print_r( $link );
}
Lars Beck
  • 3,446
  • 2
  • 22
  • 23
0

$link is out of scope. you should assign a variable when calling connectMySQL(). Find the code below.

function connectMySQL()
{
    $link = mysqli_connect("localhost", "", "", "");
    return $link;
}

function checkCredentials($username, $password)
{
    $link=connectMySQL();

    print_r($link);
}
Moussawi7
  • 12,359
  • 5
  • 37
  • 50
0

The keyword global does not make a local variable global, but rather specifies that a given name refers to a globally available variable. You can read up on PHP's variable scoping here.

That said, though, use of global is frowned upon in most -if not all- languages, and should be avoided at all costs.
Other than that, I also believe I'm right in saying that the global variables should be written at the top of the current scope:

function evilFunc()
{
    global $foo;
    $foo = 'Set value';
}
function anotherEvil()
{
    global $foo;
    echo 'I am a bad function, because I use global variables, like $foo: ', $foo;
}
$foo = 123;
anotherEvil();//will end its echo with 123
evilFunc();
anotherEvil();//echo will now show Set value instead

Bottom line: if a function needs a given instance/value to do its work: pass it as an argument. If a set of functions all need the same instances or values, wrap them in a class, and use the properties.
I've been writing PHP for over a decade now, and in that time, I've probably used global less then 5 times, and certainly less than 10 times. And if I were to write that code again today, I think I'd probably do things differently anyway...

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Always use global keyword to import global variables in functions & methods.

$link = null; // declare global

function connectMySQL() {
    global $link;      
    if (isset($link) == false) {
        $link = mysqli_connect("localhost", "", "", ""); // make global connection
    }
}

function checkCredentials($username, $password) {
    connectMySQL();
    global $link; // use global connection    
    print_r($link);
}
Krzysztek
  • 11
  • 3