1

I have a PHP database connection that works perfectly when accessed like:

$con = mysqli_connect("localhost","username","password","database");  
$result = mysqli_query($con, $query);  

I can also access it in functions by passing it as an argument:

function test($con, $args) {
    $query10 = "select name from table where id = '$args[0]'";
    $result10 = mysqli_query($con, $query10);
}

test($con, array('value1','value2'));

However, when I try to access the connection from a function that is called by another function:

mainFunction() {
    test($con, array('value1','value2'));
} 

I get "faultCode0faultStringWarning:mysqli_query() expects parameter 1 to be mysqli, null given".
Passing the variable to the mainFunction and then on to the second function works, but it creates very unreadable code with way too many parameters, because the same applies for every other variable, too.

So I tried using global variables, but for some reason they don't work. Running

$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
} 

Sum();
echo $b;  

returns 2, which leads me to suspect that something with the PHP configuration is wrong, because according to the PHP docs (http://php.net/manual/en/language.variables.scope.php) it should return 3.

How can I get global variables to work OR is there another way to access variables from nested functions without passing them on multiple times?

The PHP version is 5.3.10-1ubuntu3.8 and the Zend framework is installed, too. If you need any other information please let me know.

12
  • See 3v4l.org/dGpDl, the code works, so your premise for the question is wrong. You can either use globals, or you can pass variables, there's no other way. Globals are generally frowned upon. You should design your application around passing parameters. OOP can help a lot here instead of purely procedural functions. Commented Oct 17, 2013 at 10:33
  • @deceze It's never returning 2 like OP's experiencing on his server Commented Oct 17, 2013 at 10:35
  • @deceze That just verifies that the result I get is wrong? Bit confused about the answer. Commented Oct 17, 2013 at 10:38
  • deceze's link showing that the answer should be 3 on any PHP version, is making me very curious too. Commented Oct 17, 2013 at 10:40
  • 1
    The code you show should show 3 in any version of PHP. If it doesn't for you, that means there's more to the code than you're showing. You could make global work if you fixed whatever is wrong with the code you're not showing us. Commented Oct 17, 2013 at 10:54

2 Answers 2

1

I don't know why your global variable test didn't work, perhaps there is other code in the file you're not showing us, but global variables are generally considered a bad idea anyway.

The essence of structured programming is that you can separate out the different tasks and concerns without relying on global side-effects. Global variables which are used a lot are tricky to debug, and hard to replace with a different implementation, because all your functions expect to be able to pluck the variable out of thin air at any time.

Passing around an object representing the database connection to use is a simple form of Dependency Injection, and makes it easier to do things like unit testing.

There are other ways of getting at a database connection which aren't as thorough as Dependency Injection, but which are easier to change later than global variables. For instance, you can define a function get_db_connection, which holds a "static valuable", and returns the same connection each time it's called. Or do the same with a static variable and method in a class.

Sign up to request clarification or add additional context in comments.

Comments

0

My first suggestion is you need to concatenate array variables into your strings

$query10 = "select name from table where id = '" . $args[0] . "'";

It's easier to read and you can use this syntax with string keys as well like $args['myvar'].

The best solution to your problem is to pass the data directly to your function and have your function return those values. Globalizing your variables might work in a direct sense, but you can easily lose track of what you're doing because you've destroyed the scope of your variables.

function Sum($a, $b) {
    return $a + $b;
}
$b = Sum($a, $b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.