Jump to content

Archived

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

AdamCCFC

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

Recommended Posts

Hey guys, I'm making a website for my University project and have hit a brick wall. I want to be able to add comments to dreams that have been submitted to my database. I followed this tutorial here http://www.codewalkers.com/c/a/Database-Articles/PHPMySQL-News-with-Comments/1/ but as you can see from the title I am getting an error message. I'll show you the code I have

 

<?php
/* user config variables */
$max_items = 5; /* max number of news items to show */

/* make database connection */
$db = mysql_connect ('localhost','root','');
mysql_select_db ('dream',$db);

function displayNews($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;
    
    /* query for news items */
    if ($all == 0) {
        /* this query is for up to $max_items */
        $query = "SELECT dream_id,name,dream,tags" . 
                 "FROM dream ORDER BY date_entered DESC LIMIT $max_items";
    } else {
        /* this query will get all news */
        $query = "SELECT dream_id,name,dream,tags" . 
                 "FROM dream ORDER BY date_entered DESC";
    }
    $result = mysql_query ($query);
    while ($row = mysql_fetch_assoc ($result)) {
        /* display news in a simple table */
        echo "<TABLE border=\"1\" width=\"300\">\n";

        /* 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>'));
        
        /* display the data */
        echo "<TR><TD><b>$name</b> posted on $date</TD></TR>\n";
        echo "<TR><TD>$dream</TD></TR>\n";
        
        /* get number of comments */
        $comment_query = "SELECT count(*) FROM dreams_comments " .
                         "WHERE dream_id={$row['id']}";
        $comment_result = mysql_query ($comment_query);
        $comment_row = mysql_fetch_row($comment_result);
        
        /* display number of comments with link */
        echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=show&id={$row['id']}\">Comments</a>" .
             "($comment_row[0]}</TD></TR>\n";
        
        /* finish up table*/
        echo "</TABLE>\n";
        echo "<BR>\n";
    }
    
    /* if we aren't displaying all dream, 
     * then give a link to do so */
    if ($all == 0) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=all\">View all dream</a>\n";
    }
}

function displayOneItem($id) {
    global $db;
    
    /* query for item */
    $query = "SELECT * FROM dream WHERE 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);
    echo "<TABLE border=\"1\" width=\"300\">\n";

    /* easier to read variables and 
     * striping out tags */
    $name = htmlentities ($row['name']);
    $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
    
    /* display the items */
    echo "<TR><TD><b>$name</b></TD></TR>\n";
    echo "<TR><TD>$dream</TD></TR>\n";
    
    echo "</TABLE>\n";
    echo "<BR>\n";
    
    /* 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 dreams_id=$id";
    $result = mysql_query ($query);
    echo "Comments:<BR><HR width=\"300\">\n";
    
    /* display the all the comments */
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border=\"1\" width=\"300\">\n";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD><b>by: $name</b></TD></TR>\n";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>\n";
    
        echo "</TABLE>\n";
        echo "<BR>\n";
    }
    
    /* add a form where users can enter new comments */
    echo "<HR width=\"300\">";
    echo "<FORM action=\"{$_SERVER['PHP_SELF']}" .
         "?action=addcomment&id=$id\" method=POST>\n";
    echo "Name: <input type=\"text\" " .
         "width=\"30\" name=\"name\"><BR>\n";
    echo "<TEXTAREA cols=\"40\" rows=\"5\" " .
         "name=\"comment\"></TEXTAREA><BR>\n";
    echo "<input type=\"submit\" name=\"submit\" " .
         "value=\"Add Comment\"\n";
    echo "</FORM>\n";
     
}

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>\n";
    echo "<a href=\"{$_SERVER['PHP_SELF']}" .
         "?action=show&id=$id\">Back</a>\n";
}

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

echo "<CENTER>\n";
switch($_GET['action']) {
    
    case 'show':
        displayOneItem($_GET['id']);
        break;
    case 'all':
        displayNews(1);
        break;
    case 'addcomment':
        addComment($_GET['id']);
        break;
    default:
        displayNews();
}
echo "</CENTER>\n";
?>

 

Can anybody see why I am getting that warning?? Any help would be greatly appreciated. Thanks in advanced! Adam

Share this post


Link to post
Share on other sites

The error means your query failed. Since you did not post any additional information, such as the line number, that would pin down where in your code and which query it is, it is not directly possible to help you.

 

Best guess is that since some of the query strings are being built using concatenation (the dot . ) and there is no white-space between the list of columns and the FROM keyword, that you have an sql syntax error in the query that is failing.

 

You can echo the $query variables to see exactly what is in them for troubleshooting purposes.

Share this post


Link to post
Share on other sites

Yerr I was just about to edit it and say the warning is given on line 27  :) I shall try editing the query then, thanks!

Share this post


Link to post
Share on other sites

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