Puppy
Puppy

Reputation: 146910

PHP script won't query database

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.

Upvotes: 0

Views: 435

Answers (2)

Damp
Damp

Reputation: 3348

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?

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

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.

Upvotes: 0

Related Questions