0

I can't figure out why the variable $EMAIL is empty after I call an external script. Maybe it's because I'm accessing a different database when including login1.php? I'm unsure as to why the variable does not carry over, especially since login1.php does not re-assign the variable. Also, I'm aware mysql is being deprecated for mysqli, I'll be converting my code soon.

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 
?>
total_noob
  • 55
  • 1
  • 1
  • 6
  • Show `login1.php`. That would be helpful. – Rasclatt Feb 13 '15 at 05:25
  • I did, it's below login.php. Let me try and edit the post to make it a bit clearer. – total_noob Feb 13 '15 at 05:27
  • Oh I see this is not one big code, you just have not separated the code snippits – Rasclatt Feb 13 '15 at 05:29
  • Yeah my mistake, the code wrapping is a bit clunky, better? – total_noob Feb 13 '15 at 05:33
  • 1
    Yeah, I see now. Well one thing you should not do is use `mysql_`. Second, you may want to try and wrap your connections in a class so you can use different instances. That may work for you. – Rasclatt Feb 13 '15 at 05:37
  • Ok, so maybe structuring my code like they mentioned in this thread?: [accessing multiple databases](http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage) Maybe I should just keep the login information in the login.php files, and move the database connection code into user.php/compile.php? – total_noob Feb 13 '15 at 05:51
  • 1
    Yes, but use the PDO connections version down the page (65 votes), not the checked as correct answer. – Rasclatt Feb 13 '15 at 05:56

1 Answers1

0

Do a trace to the variable $user, I think it's never initialized. Then $userID is null and that makes the sql

SELECT * FROM users where uid='$userID'

obtain zero rows, implying $EMAIL to be unset.

javier_domenech
  • 5,995
  • 6
  • 37
  • 59