Jump to content

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given!


AdamCCFC

Recommended Posts

Hey guys, do you know why I'm getting this warning??

 

I'm getting this error on line 186 which is

 

while ($row = mysql_fetch_assoc ($result)) {

 

Here's the rest of my code:

 

			<?php

			$result = mysql_query ($query);
			while ($row = mysql_fetch_assoc ($result)) {

				/* place table row data in 
				 * easier to use variables.
				 * Here we also make sure no
				 * HTML tags, other than the
				 * ones we want are displayed */
				$date = $row['date_entered'];        
				$name = htmlentities ($row['name']);
				$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
				$tags = $row['tags'];

				/* get number of comments */
				$comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
				$comment_result = mysql_query ($comment_query);
				$comment_row = mysql_fetch_row($comment_result);

				/* display number of comments with link */
			}
		}

		function displayOneItem($id) {
			global $db;

			/* query for item */
			$query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'";
			$result = mysql_query ($query);

			/* if we get no results back, error out */
			if (mysql_num_rows ($result) == 0) {
				echo "Bad Dream ID\n";
				return;
			}
			$row = mysql_fetch_assoc($result);

			/* easier to read variables and 
			 * striping out tags */
			$name = htmlentities ($row['name']);
			$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
			$tags = ($row['tags']);
		?>

		<div class='entry'>
			<span class='link'>
				<?php echo $row['name']; ?><?php echo " dreamt "; ?><?php echo $row['dream']; ?> </a>
			</span>
		</div>	

		<div class='dream_info'>	
			<span class='vote_buttons' id='vote_buttons<?php echo $row['dream_id']; ?>'>
				<a href='javascript:;' class='vote_up' id='<?php echo $row['dream_id']; ?>'>Vote Up!</a>
				<a href='javascript:;' class='vote_down' id='<?php echo $row['dream_id']; ?>'>Vote Down!</a>
			</span>

		<span class='votes_count' id='votes_count<?php echo $row['dream_id']; ?>'><?php echo $net_vote." votes | "; ?></span>

		<?php echo "Date submitted: "; ?><?php echo $row['date_entered']; ?><?php echo " | "; ?>


		<?php 
			/* get number of comments */
			$comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
			$comment_result = mysql_query ($comment_query);
			$comment_row = mysql_fetch_row($comment_result);
			echo "<a id='commentslink' href=\"{$_SERVER['PHP_SELF']}?action=show&id={$row['dream_id']}\">Comments ($comment_row[0])</a>";#
			echo "<br />";
			echo " ";

			/* now show the comments */
			displayComments($id);
		}

		function displayComments($id) {
			/* bring db connection variable into scope */
			global $db;

			/* query for comments */
			$query = "SELECT * FROM dreams_comments WHERE dream_id=$id";
			$result = mysql_query ($query);
			echo "Comments:</br>";

			/* display the all the comments */
			while ($row = mysql_fetch_assoc ($result)) {

				$name = htmlentities ($row['name']);
				echo "<p class='name'>$name</p>";

				$comment = strip_tags ($row['comment'], '<a><b><i><u>');
				$comment = nl2br ($comment);
				echo "<p class='user_comment'>$comment</p>";

			echo "</br>";
			}

			/* add a form where users can enter new comments */
			echo "Leave a comment!";
			echo "</br>";
			echo "<form id='addcomment' action=\"" . $_SERVER['PHP_SELF'] . "?action=addcomment&id=$id\" method='POST'>";

			echo "<table border='0' width='300'";
			echo "<tr><td>Name:</td><td><input type'text' width'30' name='name'></td></tr>";
			echo "<tr><td>Comment:</td><td><textarea cols='30' rows='10' name='comment'></textarea></td></tr>";
			echo "<tr><td><input type='submit' name='submit' value='Add Comment'</td></tr>";
			echo "</form>";
			echo "</table>";
		}

		function addComment($id) {
			global $db;
			/* insert the comment */
			$query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";
			mysql_query($query);

			echo "Comment entered. Thanks!</br>";
			echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\"> Back</a>";
		}

		/* this is where the script decides what do do */

		switch($_GET['action']) {

			case 'show':
				displayOneItem($_GET['id']);
				break;
			case 'all':
				displayDreams(1);
				break;
			case 'addcomment':
				addComment($_GET['id']);
				break;
			default:
				displayDreams();
		}
	?>

You just posted a question about the same error (the line number or the code it is occurring in makes no difference), were told why you were getting it, and were told how to find out what was causing it -  http://www.phpfreaks.com/forums/index.php/topic,290968.msg1377912.html#msg1377912

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.