第二次包含后变量为空

时间:2015-02-13 05:21:11

标签: php mysql include

在调用外部脚本后,我无法弄清楚为什么变量$ EMAIL为空。也许是因为我在包含login1.php时访问了不同的数据库?我不确定为什么变量不会延续,特别是因为login1.php没有重新赋值变量。另外,我知道mysqli已经弃用了mysql,我很快就会转换我的代码。

的login.php:

<?php 
$dbhost  = 'localhost'; //login.php  
$dbname  = 'database #1';       
$dbuser  = '*';  
$dbpass  = '*';   
$appname = "*"; 

mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

?>

login1.php:

<?php //login1.php
$dbhost  = 'localhost';   
$dbname  = 'database #2';      
$dbuser  = '&';   
$dbpass  = '&';   
$appname = "&"; 

mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

?>

user.php的:

<?php //user.php

 define('DRUPAL_ROOT', '/var/www/html');
  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

global $user;
include 'login.php';
$id = $_GET['id'];
$userID = $user->uid;

//email
$query = "SELECT * FROM users where uid='$userID'";
$results = mysql_query($query);
$rows = mysql_num_rows($results);
for ($j = 0 ; $j < $rows ; ++$j) {
    $EMAIL = mysql_result($results,$j,'mail');
}
?>

compile.php:

<?php //compile.php

include 'user.php';
echo $EMAIL; //This prints the users email
include 'login1.php';
echo $EMAIL; //This prints nothing. It's an empty string. The variable $EMAIL was never called/re-assigned in login1.php 
?>

1 个答案:

答案 0 :(得分:0)

跟踪变量$ user,我认为它从未初始化。然后$ userID为null,这使得sql

SELECT * FROM users where uid='$userID'

获得零行,暗示$ EMAIL未设置。