Jump to content

Variable defined in another page not getting accessed


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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.