0

I'm using Windows 7 Ultimate, SQL Server 2008 R2, php 5.2.6 I'm new to PHP. I have this file called globalvars.php where global variables are defined.

globalvars.php

<?
  session_start();
  include_once('database.php');

  //global vars
  $_APP["SQL_DB_NAME"]="DB_NAME";
  $_APP["SQL_DB_SERVER_NAME"]="SERVER_NAME";
  $_APP["SQL_DB_USER"]="USERNAME";
  $_APP["SQL_DB_PASS"]="PASSWORD";

  $_APP["DATE_FORMAT"] = "d-m-Y";
  $_APP["TIME_FORMAT"] = "H:i";

  //generic function for SQL composition to avoid 's errors     
  function SQuoteEx($str)
  {
return "'" . str_replace("'","''",$str) . "'";
  }
  function RemoveSQuoteEx($str)
  {
return "'" . str_replace("'","",$str) . "'";
  }
  ?>

and i also have this file called database.php where the global vars are called with a snippet function that connects to the SQL Server database:

database.php

function DBConnect($sql,$debug=0)
  {
global $_APP;
//connect to db and execute query
$cnn = mssql_connect($_APP["SQL_DB_SERVER_NAME"], $_APP["SQL_DB_USER"],       $_APP["SQL_DB_PASS"]) or die(errorHandlingPage(mssql_get_last_message()));
$selected = mssql_select_db($_APP["SQL_DB_NAME"], $cnn) or die(errorHandlingPage(mssql_get_last_message())); 
$debug = 0;
//if debug mode echo sql
if ($debug) echo $sql."<br>";
//execute and return rs

$return = mssql_query($sql) or die(errorHandlingPage(mssql_get_last_message()));

if($return){
    return $return;
}   
  }

for some reason the global vars cannot be read by the database.php file. Any ideas what's going on? No message by mssql_get_last_message() was passed. Thank you.

3
  • Where are you actually calling your DBConnect function? Commented Dec 11, 2012 at 2:45
  • at the bottom of the database.php file, there are other functions where I use the DBConnect function. Commented Dec 11, 2012 at 3:19
  • So @cegfault is correct... Commented Dec 11, 2012 at 14:31

2 Answers 2

1

This is because you are including the database.php file before you are declaring the global variables. Just move include_once('database.php'); to the bottom of the declarations.

In PHP, when you include another PHP file, that file is parsed and executed immediately. You should declare any variables you need before making the include_once('database.php'); file.

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

1 Comment

i put the include_once('database.php'); code to the bottom of the declarations, but it still didn't work.
0

Which page are u calling in the browser? It looks like u will call database.php file in the browser to run the function.

Having said that, in database.php file, you should include globalvars.php on the beginning of the file.

database.php

include_once('globalvars.php');
// rest of your code...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.