Hi,

Problem At Hand: Include my config.php file which contains the string variables I want to use inside my global.php file which contains a connect() method which I am trying to use.

What's Going Wrong: It successfully is including the config.php file but it doesn't seem to have access to the variables when inside the connect() method.

Question: How do I make the method have access to these variables?

Sample config.php Code:

<?php
 $db = "db";
 $name = "name";
 $pass = "password";
 $tbl = "table";
?>

Sample global.php Code:

function connect() {
  $link = mysql_connect($db, $name, $pass);
  $dbtable = mysql_select_db($tbl);
}

It breaks at the "$link" portion of code...which leads me to believe that the variables aren't being used correctly. I also tried a print statement directly above the $link and it wouldn't print anything out.

Any help would be greatly appreciated as I am trying to further understand PhP :)

Thanks!

Recommended Answers

All 7 Replies

<?php

include('config.php'); //path to the config.php

function connect() {
  
      $link = mysql_connect($db, $name, $pass);
      $dbtable = mysql_select_db($tbl);  
	  
      }

?>

it's impossible you dont get the value from config.php... if you include it correctly

Member Avatar for Zagga

Hi,

Either place the include command inside the function, or pass the variables to the function when you call it.

connect($db, $name, $pass, $tbl);

Zagga

A function doesn't have access to variables created outside it. If a variable in a function has the same name as one outside the function, the one inside the function is still considered a different variable. When you leave the function and use the variable name again, it is the same as before you entered the function. You can read a better explanation of variable scope in PHP's manual.

You can get around this problem in a few different ways. The safest method is probably to use function arguments as Zagga mentioned, but if you want to be able to simply call connect() (without worrying about arguments and such) elsewhere in your code, you'll need another solution. You can use the global modifier to make variables inside functions refer to global variables:

connect()
{
    global $db, $name, $pass, $tbl;
    ...
}

It isn't the preferred solution, but it will allow calls to connect() to look prettier.

use this

<?php

include('config.php'); //path to the config.php

function connect() {
  
      $link = mysql_connect($db, $name, $pass);
      $dbtable = mysql_select_db($tbl);  
	  
      }
connect($db, $name, $pass,$tbl) ;
?>

awww... sorry i didn't notice it was inside the function...sorry my bad.... they were right you can either declare the variables globally, pass the variables as arguments in the function or put the include() inside the function

<?php

include('config.php'); //path to the config.php

function connect($db,$name,$pass,$tbl) {

	$link = mysql_connect($db, $name, $pass);
	$dbtable = mysql_select_db($tbl);

}

?>

or

<?php

function connect() {

	include('config.php'); //path to the config.php

	$link = mysql_connect($db, $name, $pass);
	$dbtable = mysql_select_db($tbl);

}

?>

What are these two files for? your try to connect to a database for what reason?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.