Jump to content

Variable defined in another page not getting accessed


riteshn

Recommended Posts

Hello

I have one config.php where I have defined some variables.

[code]<?php

  // Database informarion
$db_info = array("db_host" => "localhost", "db_user" => "root", "db_pwd" => "", "db_port" => 123, "db" => "preapp");

?>
[/code]

Now in another PHP page:

[code]
<?php

require('config.php');

print_r($db_info); // 1

/* This is a complete PHP page all database related stuff goes here */
function ExecuteAndGetResult ( $query )
{
print_r($db_info);  // 2
}

?>[/code]

In the above case, 1st print_r() works but the 2nd one dosnt. I believe its some GLOBAL variable defining error. What am I doing wrong?

Ritesh
[code]
<?php

require('config.php');

print_r($db_info); // 1

/* This is a complete PHP page all database related stuff goes here */
function ExecuteAndGetResult ( $query )
{
      global $db_info;
      print_r($db_info);  // 2
}

?>[/code]
Or pass it in as the second  parameter for the ExecuteAndGetResult function:
[code=php:0]require('config.php');

print_r($db_info); // 1

/* This is a complete PHP page all database related stuff goes here */
function ExecuteAndGetResult ( $query, $dbi )
{
      print_r($dbi);  // 2
}

ExecuteAndGetResult("query_here", $db_info);[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.