Need help with mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool error

Hey People

I’m trying to write an assignment but have become stuck with an error message.

Here is my code

<?php
	function dispcategories() {
		include ('dbconn.php');

		$select = mysqli_query($con, "SELECT * FROM categories");

		while ($row = mysqli_fetch_assoc($select)) {
			echo "<table class='category table'>";
			echo "<tr><td class='main-category' colspan='2'>".$row['category_title']."</td></tr>";
			dispsubcategories($row['cat_id']);
			echo "</table>";
		}

	}

	function dispsubcategories($parent_id) {
		include ('dbconn.php');
		$select = mysqli_query($con, "SELECT cat_id, subcat_id, subcategory_title, subcategory_desc FROM categories, subcategories WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id");
			echo "<tr><th width='90%'>Categories</th><th width='10%'>Topics</th></tr>";
			while ($row = mysqli_fetch_assoc($select)) {
				echo "<tr><td class='category_title'><a href='/Games Forum/topics.php?cid=".$row['cat_id']."scid=".$row['subcat_id']."'>".$row['subcategory_title']."<br />";
				echo $row['subcategory_desc']."</a><td>";
				echo "<td class='num-topics'>".getnumtopics($parent_id, $row['subcat_id'])."</td></tr>";

			}
	}

	function getnumtopics($cat_id, $subcat_id) {
		include ('dbconn.php');
		$select = mysqli_query($con, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id AND ".$subcat_id." = subcategory_id");
		return mysqli_num_rows($select);
	}
?>

Any help very much apprciated.

Ben

This is one of the most common php help questions. It has been asked and answered 10’s of thousands, or more, times on the various help forums. A web search would tell you what it means and how to find out what’s causing it.

This is not how to write functions. Adding a function definition around parts of your main code is not good programming. Creating a new database connection to run each query and running multiple queries inside of loops will quickly use all the available database connections if you have more than just some test data. Your code should make one connection and pass it into any function that needs it as a call-time parameter. Functions should also return their result to the calling code, not echo it, and you should not have the database specific code, that knows how to query for and retrieve data, inside of the html document.

Sponsor our Newsletter | Privacy Policy | Terms of Service