AdamCCFC Posted January 22, 2010 Share Posted January 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/ Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Is the query working? Instead of doing if (mysql_num_rows ($result) == 0) { do if (!$result || mysql_num_rows ($result) == 0) { // this will trigger if there was an error in the sql query or if there weren't any results } Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000114 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 Now it just says Bad Dream ID Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000115 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000116 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000118 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Try changing your query to: $query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'"; Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000123 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 Still the same :'( Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000127 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Is the echo actually displaying $id, or the value of the variable $id. It shouldn't be showing $id obviously, it should should the value. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000129 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 Ohh wait there, it's displaying it now! No idea what I did/didn't do It displays the number of comments now, but when I click the link it doesn't take me to a page, showing the dream and comment? Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000132 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Change this: echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; to echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000134 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 I changed the & to & through all the code and still doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000136 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 What does it do when you click the link. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000138 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 Just goes to the top of the page Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000141 Share on other sites More sharing options...
schilly Posted January 22, 2010 Share Posted January 22, 2010 you link is set to $_GET variable "id" "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; but you're trying to get dream_id case 'show': displayOneItem($_GET['dream_id']); break; Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000165 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 you link is set to $_GET variable "id" "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; but you're trying to get dream_id case 'show': displayOneItem($_GET['dream_id']); break; So what do I do? Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000184 Share on other sites More sharing options...
schilly Posted January 22, 2010 Share Posted January 22, 2010 well change one of them so you're getting the right variable. case 'show': displayOneItem($_GET['id']); break; Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000187 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Replace all instances of $_GET['dream_id'] with $_GET['id']. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000199 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 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 Just want to say thanks guys for helping too, it's greatly appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000205 Share on other sites More sharing options...
p2grace Posted January 22, 2010 Share Posted January 22, 2010 Can you post the latest version of your code? Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000210 Share on other sites More sharing options...
AdamCCFC Posted January 22, 2010 Author Share Posted January 22, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000211 Share on other sites More sharing options...
schilly Posted January 22, 2010 Share Posted January 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000214 Share on other sites More sharing options...
AdamCCFC Posted January 23, 2010 Author Share Posted January 23, 2010 Ok so when I do that I get an error message saying - Parse error: syntax error, unexpected T_SWITCH in C:\xampp\htdocs\mydreamz\comments.php on line 143 Line 143 then is switch($_GET['action']) { Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000219 Share on other sites More sharing options...
schilly Posted January 23, 2010 Share Posted January 23, 2010 what's the whole code there with the print_r statement? Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000222 Share on other sites More sharing options...
AdamCCFC Posted January 23, 2010 Author Share Posted January 23, 2010 <?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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000224 Share on other sites More sharing options...
schilly Posted January 23, 2010 Share Posted January 23, 2010 print_r($_GET) missing a semi-colon. Quote Link to comment https://forums.phpfreaks.com/topic/189472-why-am-i-getting-this-warning/#findComment-1000229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.