Jump to content

[SOLVED] Help! Error while looping through query results.


kjtocool

Recommended Posts

Hey guys, any help is greatly appreciated.

 

In short, I make a $databaseConnect variable earlier in the code.  I then run a query on the database, and get a results set.  I enter into a while loop to go through the results, and each time through the loop, I want to query the database again to find something else.  I use the same $databaseConnect variable.

 

I am getting an error when trying to query the database the second time through:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ...

 

<?php
		$query = "SELECT * FROM Comments WHERE article_ID = $article_ID";
		$result = mysqli_query($databaseConnect, $query);
		$row = mysqli_fetch_assoc($result);

		while ($row)
		{
			$userID = $row['user_ID'];
			$commentID = $row['comment_ID'];
			$comment = $row['comment'];

			$query2 = "SELECT username FROM Users WHERE user_ID = $user_ID";
			$result2 = mysqli_query($databaseConnect, $query2);
			$row2 = mysqli_fetch_assoc($result2);

			$userName = $row2['username'];
			mysqli_free_result($result2);

			echo '<table width="70%" border="0" align="center" cellpadding="0" cellspacing="1">';
			echo   '<tr>';
			echo	 '<td bgcolor="#F4FAFF" class="style6">Author: ' . $userName . ' ( ' . $userID . ' )</td>';
			echo   '</tr>';
			echo   '<tr>';
			echo	 '<td bgcolor="#F4FAFF" class="style6">' . $comment . '</td>';
			echo   '</tr>';
			echo '</table><br />';

			$row = mysqli_fetch_assoc($result);
		}

		mysqli_free_result($result);
		mysqli_close($databaseConnect);
		?>

 

Any explanation as to why I can't query while still using the results of the same query would be great, as well as how I can get around this.

 

KJ

Link to comment
Share on other sites

There's several things wrong like using $userID and $user_ID in second query. But you can do this in one query anyway

 

Example:

 

$query =

"SELECT *

FROM `Comments` c

JOIN `Users` u ON u.user_ID = c.user_ID

WHERE c.article_ID = $article_ID";

 

If you need to get results from 'Comments' table even when there aren't any matching 'Users', the use a "LEFT JOIN" instead.

 

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.