0

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.

8
  • 5
    You would have to share that other code you include if you want us to help debug it. There's no way $menu becomes NULL just all on its own. Even a failed mysql_connect call would result in FALSE, not NULL. Commented Feb 23, 2011 at 17:42
  • @Dan: I edited in the connection script. Commented Feb 23, 2011 at 18:18
  • It is not possible that you included that code and $menu is null unless there is additional code. I expect you failed to include that file, either by not using include/require or because you provided a wrong path and include failed. Isn't debugging in the dark fun? Commented Feb 23, 2011 at 18:22
  • The connection code is in a file named "Connection_php_mysql.htm"? Is your server set to process .htm files as PHP? Commented Feb 23, 2011 at 18:23
  • 1
    The original question was about the file with the query selecting from php_bookingtable right? That one doesn't seem to include menu.php, only db.php, which might not create a connection in a variable named $menu? You might want to start using require instead of include as a policy when including files that, if they fail to exist, would cause the rest of the code to break. They're required :) Commented Feb 23, 2011 at 19:08

2 Answers 2

1

in your function spew_bookings() you reference $menu which is not defined. In which case you would get two errors in a row thrown by php:

  1. a warning that $menu is undefined
  2. an error stating that $menu is null in your mysql_query call

Maybe you need a global $menu at the start of your function?

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

2 Comments

I cant believe we all missed that... @DeadMG: if the error location corresponds to the reference to menu in that function, then thats the problem. If you dont have an error there yet you will at some point because variables defined outside of a function cant be referenced inside without the global keyword... unless you access them through a super global like $_SESSION,$_POST,$_GLOBALS, etc.. Take a look at Variable Scope
I didn't get the warning, but did get the error. That fixed the problem- thanks.
0

First thing to do is pay attention to the error message. IT will tell you what line and what file $menu is null at. Find that line... then look at everything that happens in reverse order.

As others have said in their comments the results of your connect and select_db functions cannot produce a null value... and the variable has been defined (otherwise it would wine about it being undefined instead of null) so somewhere in the stack of calls and includes somethign is nulling out that variable.

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.