Ok so I created an instance of the mysqli class:

$siteconn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

But when I try to run a query using this I get the error that's in the title... code as follows:

$result = $siteconn->query("SELECT * FROM categories ORDER BY id LIMIT $limit");

Any help appreciated!

Thanks in advance.

Sorry I slightly decontextualised

function CreateMenu($main, $limit) {
	echo '<ul>';
	if($main == 1) {
		$limit = 4;
	} else {
		$limit = $limit;
	}

Both main and limit are defined in the function call.

If this method is defined inside the class then modify it with:

function CreateMenu($main, $limit) {
	echo '<ul>';
	if($main == 1) {
		$this->limit = 4;
	} else {
		$this->limit = $limit;
	}

or post complete code.

Not defined in a class:

function CreateMenu($main, $limit) {
	echo '<ul>';
	if($main == 1) {
		$limit = 4;
	} else {
		$limit = $limit;
	}
	$result = $siteconn->query("SELECT * FROM categories ORDER BY id LIMIT $limit");
	while($row = $result->fetch_array(MYSQL_ASSOC)) {
		$name = $row['name'];
		if($name == "shop") {
			$pageUrl = "xxx";
		} else {
			$pageUrl = $siteUrl . "/" . $name . "/";
		}
		?><li><a href="<?php echo $pageurl ?>" id="<?php echo $name ?>"<?php if($category == $name) { echo ' class="active"'; } ?>><?php echo $name ?></a></li><?php
    }
	$dbclose;
	echo '</ul>';
}

It's just called with:

<?php CreateMenu(1, 0); ?>

Let me know if you need to see any more I think that's it though.

I know it's not definitive by any stretch but the error is quoting the query line as being at fault.

So why you put this?

$limit = $limit;

If you want to show all record then remove the limit.

function CreateMenu($main, $limit) {
	echo '<ul>';
	if($main == 1) {
	$result = $siteconn->query("SELECT * FROM categories ORDER BY id LIMIT 4");
	}  
               else
	$result = $siteconn->query("SELECT * FROM categories ORDER BY id);
	while($row = $result->fetch_array(MYSQL_ASSOC)) {
		$name = $row['name'];
		if($name == "shop") {
			$pageUrl = "xxx";
		} else {
			$pageUrl = $siteUrl . "/" . $name . "/";
		}
		?><li><a href="<?php echo $pageurl ?>" id="<?php echo $name ?>"<?php if($category == $name) { echo ' class="active"'; } ?>><?php echo $name ?></a></li><?php
    }
	$dbclose;
	echo '</ul>';
}

You haven't defined $siteconn within the function.

Mostly likely you need to put global $siteconn at the top of the function.

function CreateMenu($main, $limit) {
    global $siteconn;
Comments
Worked, thanks!
Great!

The limit is defined so I can re-use the function to create the menu elsewhere with different results with a limit as the the number of items displayed.

So my first foray into using MYSQLI isn't going too well, huh?

Ok so I defined $siteconn as global and now I am getting the same error message but for the result line instead now.

Is it saying that you are trying to call a member function 'fetch_array' on non-object?

If so then something is wrong with your query. This is usually the problem. Since the query failed, an object was not returned making the function call fail.

Oh dear!

Schoolboy error there - thanks for both your help though!

Need to remember to use globals as well next time.

Thanks again!

This question has already been answered. Start a new discussion instead.