Jump to content

Why am I getting this warning?


AdamCCFC

Recommended Posts

Can anybody tell me why I am getting this warning? - Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mydreamz\comments.php on line 71

Bad Dream ID

 

The code for line 71 is -

 

    if (mysql_num_rows ($result) == 0) {

 

The whole code is here -

 

<?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 ('mydreamz',$db);

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;
    
    /* 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'";

        /* 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'];
        
        /* display the data */
        echo "<TR><TD>$name<TD><TR>";
        echo "<TR><TD>$dream<TD><TR>";
	echo "<TR><TD>$tags<TD><TR>";
        
        /* 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 */
	echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=\"{$row['dream_id']}\">'Comments'\"($comment_row[0])\"</a></TD></TR>";        
        /* finish up table*/
        echo "</TABLE>";
        echo "</br>";
    }
    
    /* 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 dreams'</a>";
    }
}

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);
    echo "<table border='1' width='300'>";

    /* 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>$name</TD></TR>";
    echo "<TR><TD>$dream</TD></TR>";
echo "<TR><TD>$tags</TD></TR>";
    
    echo "</TABLE>";
    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><HR width='300'>";
    
    /* display the all the comments */
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border='1' width='300'";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD>$name</TD></TR>";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>";
    
        echo "</TABLE<";
        echo "</br>";
    }
    
    /* add a form where users can enter new comments */
    echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>";
    echo "Name: <input type'text' width'30' name='name'></br>";
    echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>";
    echo "input type'submit' name='submit' value='Add Comment'";
    echo "</form>";
     
}

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['dream_id']);
        break;
    case 'all':
        displayDreams(1);
        break;
    case 'addcomment':
        addComment($_GET['dream_id']);
        break;
    default:
        displayDreams();
}
?>

 

Thanks in advanced for any help, Adam

Link to comment
Share on other sites

Right - which means this query is wrong "SELECT * FROM dream WHERE dream_id=$id".

 

echo $query; die(); copy the query into a msyql administration panel (e.g. phpmyadmin) and test the query.

 

Could mean 1 of 2 things.  One the query is wrong, or two $id is null.

Link to comment
Share on other sites

It gives me this error message

 

Error

 

SQL query: Documentation

 

$query = "SELECT * FROM dream WHERE dream_id=$id";

 

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "SELECT * FROM dream WHERE dream_id=$id"' at line 1

 

Link to comment
Share on other sites

Nope didn't change anything  :-\

 

I need it so when the link is clicked on the URL changes to pull the Dream and the comments from the database. But when I'm clicking on the links it just doesn't seem to be doing anything  :confused:

 

Just want to say thanks guys for helping too, it's greatly appreciated!!  :D

Link to comment
Share on other sites

Yer sure, here it is -

 

<?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 ('mydreamz',$db);

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;
    
    /* 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'";

        /* 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'];
        
        /* display the data */
        echo "<TR><TD>$name<TD><TR>";
        echo "<TR><TD>$dream<TD><TR>";
	echo "<TR><TD>$tags<TD><TR>";
        
        /* 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 */
	echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=$id\"{$row['dream_id']}\">Comments ($comment_row[0])</a></TD></TR>";        
        /* finish up table*/
        echo "</TABLE>";
        echo "</br>";
    }
    
    /* 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 dreams'</a>";
    }
}

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);
    echo "<table border='1' width='300'>";

    /* 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>$name</TD></TR>";
    echo "<TR><TD>$dream</TD></TR>";
echo "<TR><TD>$tags</TD></TR>";
    
    echo "</TABLE>";
    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><HR width='300'>";
    
    /* display the all the comments */
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border='1' width='300'";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD>$name</TD></TR>";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>";
    
        echo "</TABLE<";
        echo "</br>";
    }
    
    /* add a form where users can enter new comments */
    echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>";
    echo "Name: <input type'text' width'30' name='name'></br>";
    echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>";
    echo "input type'submit' name='submit' value='Add Comment'";
    echo "</form>";
     
}

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

 

Sorry, old code first time lol!

Link to comment
Share on other sites

looks good.

 

basically make sure all your links look correct ie. ?action=action&id=number

 

Make sure the id number is showing up in the link.

 

Next put

print_r($_GET);

above your switch statement so you know the get info looks right.

 

Also put

echo "<br>id=$id<br>";

at the top of addComment, displayComments and displayOneItem so you know when it goes in these functions and what the id is set too.

 

This should help you track down your problem.

Link to comment
Share on other sites

<?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 ('mydreamz',$db);

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;
    
    /* 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'";

        /* 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'];
        
        /* display the data */
        echo "<TR><TD>$name<TD><TR>";
        echo "<TR><TD>$dream<TD><TR>";
	echo "<TR><TD>$tags<TD><TR>";
        
        /* 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 */
	echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=$id\"{$row['dream_id']}\">Comments ($comment_row[0])</a></TD></TR>";        
        /* finish up table*/
        echo "</TABLE>";
        echo "</br>";
    }
    
    /* 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 dreams'</a>";
    }
}

function displayOneItem($id) {
    global $db;
    echo "<br>id=$id<br>";
    /* 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);
    echo "<table border='1' width='300'>";

    /* 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>$name</TD></TR>";
    echo "<TR><TD>$dream</TD></TR>";
echo "<TR><TD>$tags</TD></TR>";
    
    echo "</TABLE>";
    echo "</br>";
    
    /* now show the comments */
    displayComments($id);
}

function displayComments($id) {
    /* bring db connection variable into scope */
    global $db;
    echo "<br>id=$id<br>";
    /* query for comments */
    $query = "SELECT * FROM dreams_comments WHERE dream_id=$id";
    $result = mysql_query ($query);
    echo "Comments:<BR><HR width='300'>";
    
    /* display the all the comments */
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border='1' width='300'";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD>$name</TD></TR>";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>";
    
        echo "</TABLE<";
        echo "</br>";
    }
    
    /* add a form where users can enter new comments */
    echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>";
    echo "Name: <input type'text' width'30' name='name'></br>";
    echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>";
    echo "input type'submit' name='submit' value='Add Comment'";
    echo "</form>";
     
}

function addComment($id) {
    global $db;
    echo "<br>id=$id<br>";
    /* 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 */

print_r($_GET)

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

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.