4

I'm in learning mode here, very new to PHP, so I'm working with a code sample. Please forgive my use of 'global' here, but I want to understand php variable scoping.

Here is myGlobals.php:

<?php global $db_server; // other code not shown ?> 

Here is connectToDb.php:

<?php require_once 'myGlobals.php'; // no declared functions in this file, all inline code $db_server = mysql_connect(.....); mysql_select_db( "theDatabase", $db_server); ?> 

Here is addDbRecords.php:

<?php require_once 'myGlobals.php'; // other inline code..... doAddDeleteRecord($db_server); function doAddDeleteRecord($db_server) { //global $db_server; if( !mysql_query($query, $db_server)) { // handle the error... } } ?> 

Here is index.php:

<?php require_once 'myGlobals.php'; require_once 'connectToDb.php'; require_once 'addDbRecords.php'; // this is simplified, just trying to show that everything in inline code ?> 

Here is the problem. When I call doAddDeleteRecord($db_server) inside the file addDbRecords.php above, $db_server is not valid -- it is null -- when I call mysql_query(.., $db_server, ...) -- this is the error message:

"Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\addDbRecords.php on line 29"

So I tried using the 'global' declaration inside doAddDeleteRecord() (commented out above) -- no change.
The mysql_query(...) still fails with a NULL value for $db_server.

I know the mysql_connect(....) works because other code pulls all my records out of my database successfully (using a SELECT) and the existing records get displayed correctly in the browser.

So in my opinion, the fact that $db_server is declared with 'global' should mean that the scope of $db_server is such that once mysql_connect(...) is called -- at file scope in all my files, $db_server will be a valid connection to my database.

I'm only trying to learn about php scoping, not OOAD or anything else (for now). Why is $db_server() null here?

13
  • never use require_once. use require Commented Jun 10, 2011 at 18:12
  • 3
    @yes123: Why's that? Most of the answers here seem to claim that that's a myth. Commented Jun 10, 2011 at 18:14
  • 1
    If you still use the old mysql_ interface functions, you don't have to pass the connection handle around. If there is only one opened, it is used implicitly. (Unless you pass a null value.) Commented Jun 10, 2011 at 18:15
  • 1
    @require_once: It might indeed be a cause of the troubles here, because the myglobals.php script is really just loaded once, where OP might have intended it to be included multiple times. (The general disadvisal makes no sense however.) Commented Jun 10, 2011 at 18:16
  • 1
    @mario: except that the OP isn't using include[_once] inside of a function. The OP is including it at the top of each file, which no matter how you cut it would still require the use of the global modifier to use those variables within functions. Commented Jun 10, 2011 at 18:27

2 Answers 2

5

So, you have:

<?php global $db_server; // other code not shown ?> 

and you include this where needed:

require_once 'myGlobals.php'; // other inline code..... doAddDeleteRecord($db_server); 

The problem is that if you already included 'myGlobals.php' elsewhere, it won't be included here. So you can't guarantee that the global will be brought into scope.

Instead, write:

require 'myGlobals.php'; // other inline code..... doAddDeleteRecord($db_server); 

Or take the much better approach of just:

// other inline code..... doAddDeleteRecord($GLOBALS['db_server']); 
Sign up to request clarification or add additional context in comments.

5 Comments

+1 you use global inside a function to tell PHP you want to use a globally scoped variable instead of creating a privately scoped variable that is only visible in the function.
I disagree, I have a four-file set-up working right now and they all use require_once(). The very first time you execute the statement global $db_server is sufficient.
@Darien: Only if you always include the file at the global scope.
Actually the problem with my answer is that the global isn't required at all in any of his files.
That's a problem too, but what I mean is that the side-effects of global $foo;, when (pointlessly) done in global scope, are also global... as long as it has been included once, early enough, you never need to include it again. It's the same as if you had defined a function or class in global scope.
1

I think there's a concern with scope hiding; that is, you're using the same name ($db_server) for the global and for the function local variable. The function local scope hides the global variable name. If you have a global, you don't need to pass it to your function; if you do, don't use the same name for it.

1 Comment

I changed the variable name to "function doAddDeleteRecord($db_server_global_scope)" and used db_server_global_scope in the call to mysql_query(.., $db_server_global_scope, ...) -- no change. Just FYI, I did not use 'global' anywhere before, only added it to try to fix this. If I remove 'global' everywhere it's still the same problem. Note: the call to doAddDeleteRecord($db_server_global_scope) that is failing is when my 'add record' form posts back to the file -- could the fact that I'm re-entering the code due to a POST somehow make the db_server null?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.