0

I am breaking my head why the simplest thing is not working.

All I want is to access the variable declared in the included file.

I have file called connection.php in which I have a class called MySqlDatabase. In the bottom of the file I created the instance of the class and assiged it to variable called $database

// filename database.php begin class MySqlDatabase(){ // code goes here } $database = new MySqlDatabase(); // filename database.php end 

Now I included database.php in something.php and trying to access the $database variable like this

//something.php require_once 'database.php'; function foo(){ global $database; $sql = "some sql statement"; mysql_query($sql,$database->connection); //and remainig code goes here... } 

When I run the something.php it is expected that the global variable $database should be available in the function foo() but it seems like the variable is null, I tested with is_object() function to check the object is available but it returning false.

For debugging purpose I added is_object() check in the database.php file and it is returning true.

Also I tried to access the global variable using other options like

$database =& $GLOBALS['database']; 

But I still have no luck. I then printed the complete $GLOBALS array using print_r() function also but it doesnt have the $database variable, I also checked using get_defined_vars() but it also doesnt have the variable. But I am able to see them in the included file itself

I am breaking my head since two days to make this simple thing work but I compromised and copy pasting database connection code in all the files.

1
  • $database is not a global variable as such, you are essentially just making the variable $database available whilst using the foo() function. Could we see the full MySqlDatabase() class, then the implementation of the class will become more clear. Commented Oct 5, 2011 at 11:08

5 Answers 5

2

I made some change in your code, this work for me.

database.php

<?php // filename database.php begin class MySqlDatabase { public $connection; function MySqlDatabase() { $this->connection= mysql_connect('HOST','USER','PASSWORD'); mysql_select_db('databaseName',$this->connection); } } $database = new MySqlDatabase(); // filename database.php end ?> 

something.php

<?php require_once 'database.php'; function foo( ) { global $database ; $sql = "select * from tableName"; $rs=mysql_query($sql,$database->connection); while($row=mysql_fetch_array($rs)) { print_r($row); } //and remainig code goes here... } foo(); ?> 

You might be manipulating $connection variable in wrong manor.

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

Comments

1

There are only 3 possible reasons

  • $database defined not in the global scope
  • database.php being included not as a file
  • some typo invisible to reader

Comments

1

You could use a singleton pattern or set the variable static in database class.

Comments

1

If you have included the file then use the following:

//something.php require_once 'database.php'; /* Function FOO starts a new scope and doesn't see database object declared in database.php * Therefore, you pass on the database object from calling function and use that instead. */ function foo($dbObj){ $sql = "some sql stagement"; mysql_query($sql,$dbObj->connection); //and remainig code goes here... } foo($database); 

Comments

0

Well, I figured out the problem. The issue is with the amfphp framework. This framework includes all my files (something.php). Its hard to explain it but in simple words I included the database.php in the one of the main php file related to the amfphp framework.

Thanks for you suggetions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.