PDA

View Full Version : Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource


mr_badger
02-05-2007, 01:53 PM
I'm doing a tutorial from a book and have typed in the code the same as whats in the book, everything is going ok apart from this line of code, I can't figure out what I have done wrong.

This is the code
1.$sql = "SELECT entries.*, categories.cat FROM entries, categories
2.WHERE entries.cat_id = categories.id
3.ORDER BY dateposted DESC
4.LIMIT 1;";
5.$result = mysql_query($sql);
6.$row = mysql_fetch_assoc($result);
7.echo "<h2><a href='viewentry.php?id=" . $row['id']
8.. "'>" . $row['subject'] .
9. "</a></h2><br />";
10. echo "<i>In <a href='viewcat.php?id=" . $row['cat_id']
11. ."'>" . $row['cat'] .
12. "</a> - Posted on " . date("D jS F Y g.iA",
13. strtotime($row['dateposted'])) .
14. "</i>";
15. echo "<p>";
16. echo nl2br($row['body']);
17. echo "</p>";
18.
19. echo "<p>";
20.
21.$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] .
23. "ORDER BY dateposted;";
$commresult = mysql_query($commsql);
25. $numrows_comm = mysql_num_rows($commresult);
if($numrows_comm == 0) {
echo "<p>No Comments.</p>";
}
else {
echo "(<strong>" . $numrows_comm . "</strong>) comments : ";
$i = 1;
while($commrow = mysql_fetch_assoc($commresult)) {
echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i .
"'>" . $commrow['name'] . "</a> ";
$i++;
}
}
echo "</p>";

It's line 25 in bold that is causing the problem, any ideas?

davidj
02-05-2007, 02:26 PM
you have omitted the numbers yes?

the line numbers would make the script invalid

also this 25 is invalid

25. $numrows_comm = mysql_num_rows($commresult);

Im guessing that you have copied this script which inluded the line numbers

mr_badger
02-05-2007, 06:27 PM
Yes I typed the numbers in and copied the script.

davidj
02-05-2007, 07:11 PM
so does the script your trying to run contain line numbers??

if it does then you need to remove them

mr_badger
02-05-2007, 10:45 PM
no, the script doesn't contain line numbers.

davidj
02-05-2007, 10:47 PM
paste the code as you have it in dreamweaver

mr_badger
02-06-2007, 03:42 PM
<?php

require("header.php");

$sql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<h2><a href='viewentry.php?id=" . $row['id']
. "'>" . $row['subject'] .
"</a></h2><br />";
echo "<i>In <a href='viewcat.php?id=" . $row['cat_id']
."'>" . $row['cat'] .
"</a> - Posted on " . date("D jS F Y g.iA",
strtotime($row['dateposted'])) .
"</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";

echo "<p>";

$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] .
"ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);
if($numrows_comm == 0) {
echo "<p>No Comments.</p>";
}
else {
echo "(<strong>" . $numrows_comm . "</strong>) comments : ";
$i = 1;
while($commrow = mysql_fetch_assoc($commresult)) {
echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i .
"'>" . $commrow['name'] . "</a> ";
$i++;
}
}
echo "</p>";
$prevsql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1, 5;";
$prevresult = mysql_query($prevsql);
$numrows_prev = mysql_num_rows($prevresult);
if($numrows_prev == 0) {
echo "<p>No previous entries.</p>";
}
else {
echo "<ul>";

while($prevrow = mysql_fetch_assoc($prevresult)) {
echo "<li><a href='viewentry.php?id="
. $prevrow['id'] . "'>" . $prevrow ['subject']
. "</a></li>";
}
}

require("footer.php");

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

davidj
02-06-2007, 03:50 PM
you are echoing allot of information before the HTML is sent

this means yo have no control where this echo is going to appear other than at the top of the document.

these echos are also generating HTML tags which will render outside of the <html.... ... /html> tags

This will invalidate the document

you have copied this script but you dont really know what you have copied or how this works. You need to understand the basics first

try the vids
http://www.dreamweaverclub.com/vtm

mr_badger
04-17-2007, 02:55 PM
It's been a while since this thread was started but I still can't get this to work.
I'am not echoing any code before the html now but I'am still getting the error:
mysql_num_rows(): supplied argument is not a valid MySQL result resource

<?php

require("header.php");

$sql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<h2><a href='viewentry.php?id=" .$row['id']
. "'>" . $row['subject'] .
"</a></h2><br />";
echo "<i>In <a href='viewcat.php?id=" . $row['cat_id']
."'>" . $row['cat'] .
"</a> - Posted on " . date("D jS F Y g.iA",
strtotime($row['dateposted'])) .
"</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";

echo "<p>";

$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] .
"ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);
if($numrows_comm == 0) {
echo "<p>No Comments.</p>";
}
else {
echo "(<strong>" . $numrows_comm . "</strong>) comments : ";
$i = 1;
while($commrow = mysql_fetch_assoc($commresult)) {
echo "<a href='viewentry.php?id=" . $row['id'] ."#comment" . $i .
"'>" . $commrow['name'] . "</a> ";
$i++;
}
}
echo "</p>";
$prevsql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1, 5;";
$prevresult = mysql_query($prevsql);
$numrows_prev = mysql_num_rows($prevresult);

if($numrows_prev == 0) {
echo "<p>No previous entries.</p>";
}
else {
echo "<ul>";

while($prevrow = mysql_fetch_assoc($prevresult)) {
echo "<li><a href='viewentry.php?id="
. $prevrow['id'] . "'>" . $prevrow {'subject'}
. "</a></li>";
}
}

echo "</ul>";

require("footer.php");

?>The error is coming from line 38 which is this:
$numrows_comm = mysql_num_rows($commresult);

Hope someone can help as this has been going on to long now. Thankyou.

davidj
04-17-2007, 06:10 PM
its not an error its a warning

supress it by adding an @

example...

$numrows_comm = @mysql_num_rows($commresult);