0

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.

3
Contributors
12
Replies
22
Views
8 Years
Discussion Span
Last Post by anarki2k3
Featured Replies
  • You haven't defined $siteconn within the function. Mostly likely you need to put [icode]global $siteconn[/icode] at the top of the function. [code] function CreateMenu($main, $limit) { global $siteconn; [/code] Read More

0

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.

0

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.

0

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.

0

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>';
}
2

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;
Votes + Comments
Worked, thanks!
Great!
0

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.

0

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.

0

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.

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.