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

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;
.....

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

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

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.