Jump to content

Need help with error Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in E:\Utilities\xampp\htdocs\Games Forum\content_function.php on line 20


Ben555

Recommended Posts

Hi Guys.

 

Doing an assignment for uni, and stuck on an error. Ill attach some files to show the problem and any help very much appreciated.

 

Error and the screen it comes on

 

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);
    }
?>

 

 

and the structure of the database

 

 

 

 

 

 

 

error screen.png

topics structure.png

Edited by Ben555
Link to comment
Share on other sites

Hi and thanks. I am a new user so I apologise if I have breached forum etiquette.

The apache logs show only this

Sat Mar 28 18:43:39.275738 2020] [php7:warn] [pid 23828:tid 1924] [client ::1:53013] PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in E:\\Utilities\\xampp\\htdocs\\Games Forum\\content_function.php on line 20

Line 20 is the while statement in the code below

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

            }

 

Im really very new at this and don't really know what Im doing, hence why I posted.

 

Its an attempt to write a discussion forum for an assignment. The content_function.php is meant to display the name of the topics, sub topics and how many posts there are. I seem to have managed to print out the main topic headings but failed to display the rest.

Edited by Ben555
Link to comment
Share on other sites

Add some checking in your code to be sure that the query was run successfully.  Something like:

	// check if there was an error running the query
	if (!$select)
	 {    
	     echo "Error running query : " . mysqli_error($con);
	    exit();
	}
	

I'll let you figure out where to place this block of code

This will print out a message if the query is failing you.  Or if the $con did not get properly made prior to that.

Suggestion.   Always assign your query statement to a variable and then use the variable in the query call.  That way you can have your error handling code print out the query for your review.  As you have it now you can't do that.

Link to comment
Share on other sites

Ok thank you!

 

This is what I get back

Error running query : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

Shooters

 

Unfortunately it now refers to line 1, and I really cant find a syntax error. Nor is there a " on line 1.

Im sure ive made a silly mistake somewhere. 

Link to comment
Share on other sites

this is line 18 and im pretty sure the error is actually here somewhere.

$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");

 

not sure I have the syntax right

Link to comment
Share on other sites

Try this:

$queryString="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 $queryString;
$select = mysqli_query($con,$queryString);

Look at the echo'ed string to find the syntax error.

Link to comment
Share on other sites

The message is correct.  The problem is in the query.  If you did it the way I said to do previously, you echo out the query itself with your error message and look at it.  YOu have some bad logic in that query.

Do it this way:

$q = "SELECT cat_id, subcat_id, subcategory_title, subcategory_desc 
		FROM categories c, subcategories s 
		WHERE ($parent_id = categories.cat_id) AND 
			($parent_id = subcategories.parent_id");
$select = mysqli_query($con, $q);
if (!$select)
{
	echo "Error running query - $q<br>", mysqli_error($con);
	exit();
}

       Note how I wrote the query to make it more readable.  And how I saved the query itself so that it can be echo-ed out to review.  Your problem may very well be that you have two tables but you don't distinguish which selections are from which table.  If you have fields with the same name in both tables the query engine doesn't know which one you want.  Note how I assigned aliases in the query to the table names.  Add those aliases to your field names by doing "c.cat_id" or "s.cat_id" depending which table the field comes from.

And you are missing a closing paren.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.