I am creating a website and have run into a problem. I am trying to keep my code clean and not repeat things much. I am attempting to do a database query and use a while loop to go through the results and output them.
I am using 3 main files to do this, a database connection file, a session register file and the main file which is going to be the accessable file to the public. I have included the 3 files below and the error message i am receiving. I have done alot of searching on the topic and have not found a solution. I get the page to load without errors when i include
$query = "SELECT * FROM buildings";
$build = mysql_query($query);
into the admin.php page from the session.php page. But when i use an include it won't work.
db.php
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());
?>
session.php
<?php
session_start();
echo"<a href='../war/signup.php'>Signup</a> | <a href='../war/index.php'>Login</a> | <a href='../war/index2.php'>Member Zone</a> | <a href='../war/logout.php'>Logout</a>";
include('db.php');
if(session_is_registered('user_id'))
{
echo'<p>You are logged in as '. $_SESSION['username']. ' with user id '. $_SESSION['user_id']. '.</p>';
$user_id = $_SESSION['user_id'];
// START Buildings Table Accociation Array
$query = "SELECT * FROM buildings";
$build = mysql_query($query);
// START Buildings Table Accociation Array
}
else
{
echo "<p>You are not Logged in.</p>";
}
?>
admin.php
<?php
include('session.php');
while ($row = mysql_fetch_array($build, MYSQL_ASSOC))
{
//processing code
}
?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*********/public_html/war/includes/admin.php on line 41
Line 41 is : while ($row = mysql_fetch_array($build, MYSQL_ASSOC))