Skip to main content
Put error message in blockquote and put in inline code formatting
Source Link

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)doAddDeleteRecord($db_server) inside the file addDbRecords.php above, $db_server$db_server is not valid -- it is null -- when I call mysql_query(.., $db_server, ...)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"

"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()doAddDeleteRecord() (commented out above) -- no change.
The mysql_query(...)mysql_query(...) still fails with a NULL value for $db_server$db_server.

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

So in my opinion, the fact that $db_server$db_server is declared with 'global' should mean that the scope of $db_server$db_server is such that once mysql_connect(...)mysql_connect(...) is called -- at file scope in all my files, $db_server$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()$db_server() null here?

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 SELECCT) 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?

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?

Source Link
wantTheBest
  • 1.7k
  • 5
  • 46
  • 71

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 SELECCT) 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?