0

Hi,

I can't solve this problem:

I have a query which fetches the contents of the page meta tags from a database. It works fine when I have the query on page but when I turn it into a function (which is placed in my functions file) and call it from the page it does not work. Here is the code:

function get_meta_keywords() {
	$q = "SELECT meta_tags FROM biology_notes WHERE section='2.1-cell-theory'";
	$r = @mysqli_query($dbc, $q);
	$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
	$meta_keywords = $row['meta_tags'];
	return $meta_keywords;
}

When I use it in this way I get the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Users/oscarconiel/Sites/IB_guides/includes/page_setup_functions.php on line 33

Anyone know why it doesn't work?

Thanks

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by johny_d
0

It's probably because you don't have the $dbc available inside that function; you have to either pass it as a parameter to the function
like: function($dbc) { ....
or set it as a global variable inside that function
like: function() {
global $dbc;
.....

0

It's probably because you don't have the $dbc available inside that function; you have to either pass it as a parameter to the function
like: function($dbc) { ....
or set it as a global variable inside that function
like: function() {
global $dbc;

Oh right.... I didn't know I needed to pass it into the function. I thought that having it defined just above the function would work... I'm new to PHP (an programming in general) so I guess it's a bit of a stupid mistake.

Thanks

Edited by Conielo: n/a

0

You're welcome; do a search for "php variables scope" so you learn where and how variables are available.

Cheers

This topic has been dead for over six months. 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.