Hi
I have spent days trying to figure this out, and am my wits end. I have an include file which essentially get all the entries from a database table which contains two columns - cat_id and cat_name.
I connect to this include file several times from different parts of the program using $_SERVER[‘DOCUMENT_ROOT’].
Here is the include file:
//get all categories
$result = $link->query('SELECT cat_id, cat_name FROM category');
if (!$result) {
$error = 'Error fetching category details.';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
exit();
}
while ($row = mysqli_fetch_array($result)) {
$categories[] = array('cat_id' => $row['cat_id'], 'cat_name' => $row['cat_name']);
}
The code snippet below works:
ini_set('memory_limit', '-1');
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../code4/common.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../code4/db.inc.php';
checkUser();
//Show form to add new subcategory
if (isset($_GET['add'])) {
$pagetitle = 'New Sub-Category';
$sideNav = '<p><a href="../logout.php">Always Logout when finished</a></p>
<p><a href="../index.php">Return to CMS Home</a></p>
<p><a href="">Edit existing Sub-Category</a></p>';
$catHead = 'Select category to associate with subcategory type:';
$action = 'addform';
$subcat_id = '';
$cat_id = '';
$subcat_code = '';
$subcat_name = '';
$title_txt = '';
$meta_description = '';
$meta_keywords = '';
$top_txt = '';
$bot_txt = '';
$add_info = '';
$add_info_link = '';
$specs = '';
$foot_txt = '';
$table_type = '';
$button = 'Add Sub-Category';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/getAllCategories.php';
include 'form.html.php';
exit();
}
However, the snippet following is the one I am having trouble with. When I connect to this part of the script, I get the error message: “[28-Aug-2012 20:35:13] PHP Fatal error: Call to a member function query() on a non-object in /Users/cliffgs/Sites/goldenfields.dev/includes/getAllCategories.php on line 4”.
But when I run the script shown above, which connects to the very same include file, it works. I simply do not know what is going on. I have tried explicitly outputting this php errors, but they just tell me that it a string, not an object. But that doesn’t really help me, because I cannot understand why it should be different considering I am always using $link as the database object, and it works in one (and other) situations, but not in this one. Can someone help me with what I am obviously missing.
ini_set('memory_limit', '-1');
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../code4/common.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../code4/db.inc.php';
checkUser();
//Show form to add new product
if (isset($_GET['add'])) {
$pagetitle = 'New Product';
$sideNav = '<p><a href="../logout.php">Always Logout when finished</a></p>
<p><a href="../index.php">Return to CMS Home</a></p>
<p><a href="">Edit existing Product</a></p>';
$catHead = 'Select category to associate with subcategory type:';
$action = 'addform';
$id = '';
$cat_id = '';
$subcat_id = '';
$pID = '';
$orderedBy = '';
$description = '';
$short_desc = '';
$coverage = '';
$weight = '';
$price = '';
$priceINTL = '';
$info = '';
$manu = '';
$link = '';
$button = 'Add new Product';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/getAllCategories.php';
// include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/getProductSubcat.php';
include 'form.html.php';
exit();
}
I should also add, that I have changed the offending script so that instead of including the the getAllCategories file, I just pasted in that same code. It would therefore appear that the problem is with that script, but I cannot see where.
Many thanks