...

View Full Version : Global Array not Working?



dniwebdesign
10-26-2006, 05:58 PM
Alright in my config.inc.php I got


$db["username"] = "user123";
$db["password"] = "likeimleavingthisin";
$db["host"] = "localhost";
$db["name"] = "stats";
$db["prefix"] = "prefix";

and in my scripts.class.php I have


function login() {
global $db;
echo ">".$db["username"]."<";
return 0;
}

However when I call the function in a different page all I get is ><. I have had this happen before and couldn't explain it. It only happens with the arrays and not a variable like $db_user. What's wrong?

dniwebdesign
10-28-2006, 11:18 PM
^bumpers^

marek_mar
10-29-2006, 12:06 AM
The code works...

dniwebdesign
10-30-2006, 01:43 AM
Well, as I said... I only get ><. Unless there is a setting in PHP that is not allowing me to do it otherwise.

Fumigator
10-30-2006, 01:47 AM
Is the assignment of $db['username'] in a function, and do you have global $db in that function?

Put a print_r($db) at the top of login() and see what that gives you.

dniwebdesign
10-30-2006, 01:58 AM
The $db array values are in config.inc.php (which isn't a class and has not functions). The function wanting access to the array is in a class.

I used the print_r and it returned a 1.

CFMaBiSmAd
10-30-2006, 02:13 AM
Is the function just a function or is it part of a class definition? I think we need more information about the organization of your code, what files are included where? If this is part of a class definition, include enough of the code so that someone could duplicate and test this.


I call the function in a different pageDoes this different page also directly include the config.inc.php or does it include a page that then includes the config.inc.php?

Even though this issue should be solved to gain an understanding of why it is not working, these values are essentially constants and it would be better to use a define(...) statement. This will also avoid the scope/global issue. A defined constant can be used in any code that it has been included into with out the need for the global keyword within functions.

dniwebdesign
10-30-2006, 04:46 AM
I'll give a shot using the constants...

CFMaBiSmAd
10-30-2006, 05:16 AM
On a PHP5 system the orginal concept you described works as expected -

main code -

<?php
include "config.inc.php"; // include $db[...] variables
include "scripts.class.php"; // include the class definition

$mabismad = new mabismad; // create an instance of the class
$mabismad->login(); // call the login() member function
?>The config.inc.php file -

<?php
$db["username"] = "user123";
$db["password"] = "likeimleavingthisin";
$db["host"] = "localhost";
$db["name"] = "stats";
$db["prefix"] = "prefix";
?>The scripts.class.php file -

<?php
class mabismad {

function login() {
global $db;
echo ">".$db["username"]."<";
return 0;
}
}
?>Produces this output -

>user123<I suspect that if this did not work it is either because the config.inc.php file does not have <?php ?> tags or it was not included in the code where the instance of the class was used?

dniwebdesign
10-30-2006, 03:46 PM
Well, it was included and called because when I had

<?php
$db["username"] = "user123";
$db["password"] = "likeimleavingthisin";
$db["host"] = "localhost";
$db["name"] = "stats";
$db["prefix"] = "prefix";
$prefix = "prefix";
?>

the $prefix variable worked just as it's suppose to, it's just the $db array that doesn't want to seem to go global.

Fumigator
10-31-2006, 01:47 AM
I used the print_r and it returned a 1.

Well the array $db is getting stomped somewhere down the line then, which you probably don't need me telling you! Not something I could really debug without the actual code/environment... good luck.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum