wantTheBest
wantTheBest

Reputation: 1720

using 'global' in php

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?

Upvotes: 4

Views: 504

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

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']);

Upvotes: 5

Paul Sonier
Paul Sonier

Reputation: 39520

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.

Upvotes: 1

Related Questions