I've got a small PHP script that just connects to the database and dumps a certain table to the screen.
// Replaced with expanded snippet below
This code throws an error saying that $menu is NULL. But an identical script
// Also replaced with expanded snippet below
executes fine- and they include the same file to obtain $menu with the same connection characteristics. The mysql_connect and mysql_select_db functions are all called as func() or die(mysql_error()) and I haven't had any errors back from them. Any idea what the cause could be?
Here's my connection code:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_menu = "";
$database_menu = "";
$username_menu = "";
$password_menu = "";
$menu = mysql_connect($hostname_menu, $username_menu, $password_menu) or die(mysql_error());
$menu_db = mysql_select_db($database_menu, $menu) or die(mysql_error());
?>
I redacted the variables. I've checked, and there is no file shadowing or using $menu for other purposes.
Here's the index page. I didn't write this, just so you know.
<?PHP
session_start();
if($_SESSION['login'] != "true"){
header("Location: login.php");
}else{}
?>
<?PHp include("includes/top.php"); ?>
<?PHP include("includes/menu.php"); ?>
<div id="main_page">
<div id="page2">
<?PHP
if($_REQUEST['page'] == ""){
include("pages/index.php");
}else{
include("pages/".$_REQUEST['page'].".php");
}
?>
</div>
</div>
<?PHP include("includes/bottom.php"); ?>
The other PHP includes are actually plain HTML and don't define any additional variables. The first code snippet above expands to
<!-- semester1.php -->
<?php
include('includes/db.php');
function spew_bookings() {
$roomquery = "SELECT * FROM php_bookingtable";
$result = mysql_query($roomquery, $menu);
while($row = mysql_fetch_assoc($result)) {
foreach($row as $key => $value) {
echo $key; echo '<br>'; echo $value;
}
}
}
function spew_user_bookings(){
}
if ($_SESSION['deptid'] == 'Central Administrator') {
spew_bookings();
} else {
spew_user_bookings();
}
?>
and is included from the index.php above. The other file is redirected, not included, and looks like this:
<?PHP
session_start();
include("../includes/db.php");
$loginQuery = "SELECT * FROM php_usertable WHERE username = '".$_REQUEST['username']."'";
$loginResult = mysql_query($loginQuery, $menu);
$loginRow = mysql_fetch_array($loginResult);
$password = $_REQUEST['password'];
//-- get their department from their department id --//
$deptQuery = "SELECT * FROM php_departmenttable WHERE deptid = '".$loginRow[3]."'";
$deptResult = mysql_query($deptQuery, $menu);
$deptRow = mysql_fetch_array($deptResult);
if(md5($password) == $loginRow[2]){
//-- set all the users information into sessions --//
$_SESSION['username'] = $loginRow[1]; //-- from usertable --//
$_SESSION['deptid'] = $deptRow[1]; //-- from depttable --//
$_SESSION['extension'] = $loginRow[4];
$_SESSION['email'] = $loginRow[5];
$_SESSION['login'] = "true";
header("Location: ../index.php");
}else{
header("Location: ../login.php");
}
?>
I know that there are security holes in here, but they're not a concern right now.
$menubecomesNULLjust all on its own. Even a failedmysql_connectcall would result inFALSE, notNULL.$menuis null unless there is additional code. I expect you failed to include that file, either by not usinginclude/requireor because you provided a wrong path andincludefailed. Isn't debugging in the dark fun?php_bookingtableright? That one doesn't seem to includemenu.php, onlydb.php, which might not create a connection in a variable named$menu? You might want to start usingrequireinstead ofincludeas a policy when including files that, if they fail to exist, would cause the rest of the code to break. They're required :)