在php中定义全局变量

在php中定义全局变量,php,sql-server-2008-r2,Php,Sql Server 2008 R2,我使用的是Windows7Ultimate、SQLServer2008R2、PHP5.2.6 我是PHP新手。我有一个名为globalvars.php的文件,其中定义了全局变量 globalvars.php <? session_start(); include_once('database.php'); //global vars $_APP["SQL_DB_NAME"]="DB_NAME"; $_APP["SQL_DB_SERVER_NAME"]="SERVER_

我使用的是Windows7Ultimate、SQLServer2008R2、PHP5.2.6 我是PHP新手。我有一个名为globalvars.php的文件,其中定义了全局变量

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) . "'";
  }
  ?>
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;
}   
  }
include_once('globalvars.php');
// rest of your code...

这是因为在声明全局变量之前包含了
数据库.php
文件。只需移动
include_once('database.php')到声明的底部


在PHP中,当您
包含另一个PHP文件时,会立即解析并执行该文件。在创建
include_once('database.php')之前,应该声明所需的任何变量文件。

您在浏览器中调用哪个页面?看起来您将在浏览器中调用database.php文件来运行该函数

话虽如此,在database.php文件中,应该在文件开头包含globalvars.php

database.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) . "'";
  }
  ?>
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;
}   
  }
include_once('globalvars.php');
// rest of your code...

在database.php文件的底部,还有我使用DBConnect函数的其他函数。因此@cegfault是正确的…我把include_放了一次('database.php');代码到声明的底部,但仍然不起作用。