Jump to content

Why am I getting this warning?


AdamCCFC

Recommended Posts

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mydreamz\index.php on line 224??

 

Here's my code -

 

<?php
/**
Display the results from the database
**/
		function displayDreams($all = 0) {
			/* bring in two variables
			 * $db is our database connection
			 * $max_items is the maximum number
			 * of news items we want to display */
			global $db, $max_items;

$q = "SELECT * FROM dream ORDER BY date_entered DESC";
$r = mysql_query($q);

if(mysql_num_rows($r)>0): //table is non-empty
while($row = mysql_fetch_assoc($r)):
	$net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>

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


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



</div>

<?php echo $row['tags']; ?><?php echo " "; ?><?php echo $row['date_entered']; ?><?php echo "</br>"; ?>
<?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>";



?>

<?php
endwhile;
endif;
?>
   
   
	<?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']);

			/* display the items */
			echo "<h1>" . $row['name'] . "</h1>";
			echo 'dreamt';
			echo "<p class='dream'>" . $row['dream'] . "</p>";				  
			echo "<p class='tags'>" . $row['tags'] . "</p>";
			echo "<p class='date'>" . $row['date_entered'] . "</p>";
			echo "</br>";

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

 

This is line 223 -

 

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

 

 

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.