"Call to a member function query() on a non-object" when it works elsewhere?

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

$link = ‘’;

You have this line in the second snippet.

Ye Gods I cannot believe that!!! Thanks Guido, you have saved my life, even though I still feel stupid! Of course “link” is a column in the product table, but I never gave that a thought.

Thanks very much for spotting that for me. Maybe, sometime in the next ten years I may have seen it, but I am not sure.

Thank you.

The clue was in the error message.

“Call to a member function query() on a non-object”

So you should have looked for ->query(‘some stuff in here’).

When you found that, you’d have seen it was being called on a $link variable which is supposed to be an object, but it is not?

So what is it?

So you’d do this:

var_dump($link);

Which would have likely said something like:


var link string 0 "";

So then you’d go looking for where $link was defined.

I’m not trying to rub it in, just explaining how you would interpret this particular error message so that you’ve got more chance of fixing it quicker.

Thanks Cups, I know you are not rubbing it in, I take all the help I can. Once the cause of the error was pointed out to me by Guido2004 I felt so stupid. I had been looking in every direction but the correct one. Still, that is yet another mistake I won’t make again.

Thank you for your comment.