php mysql count
Kai
- php
Hallo liebe Leute im Forum, folgende Problematik, ich versuche die Anzahl an Kommentaren aus einer mysql Datenbank nach einem bestimmten Wert zu Zählen, hier $bid nur scheiter ich zur Zeit kläglich, hier mal mein Versuch, wobei der untere Teil wohl von mehr Bedeutung ist:
<?php
$res = mysql_query ("SELECT bid, autor, content,thema, stamp FROM blog_index");
$num = mysql_num_rows($res);
echo mysql_error();
for ($i=0; $i<$num; $i++)
{ $bid = mysql_result($res, $i, "bid");
$autor = mysql_result($res, $i, "autor");
$thema = mysql_result($res, $i, "thema");
$content = mysql_result($res, $i, "content");
$stamp = mysql_result($res, $i, "stamp");
$datum = date("d.M.Y",$stamp);
$uhrzeit = date("H:i",$stamp);
echo"<h1>$thema <span>$autor  |   $datum   $uhrzeit</span></h1>";
echo nl2br ("<p>$content</p>");
echo"<p><a href="content_comment.php?bid=$bid">Kommentar</a></p>";
$res = mysql_query ("COUNT(bid) FROM blog_comment WHERE bid = '$bid' GROUP BY bid");
$row = mysql_fetch_row($res);
echo $row;
}
?>
Hi,
probier die Zeile mal so:
$res = mysql_query ("SELECT COUNT(bid) FROM blog_comment WHERE bid = '$bid'");
Gruß
Andi
Danke für eure schnellen Antworten, aber jetzt kommt folgende Fehlermeldung:
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 6
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 7
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 8
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 9
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 10
Hallo,
also das ist ein richtiger Wurschtelcode. Ich würde das so machen:
$qry = "SELECT bid, autor, content,thema, stamp FROM blog_index";
if (($res = mysql_query($qry)) && mysql_num_rows($res))
{
while ($row = mysql_fetch_object($res))
{
$bid = $row->bid;
$autor = $row->autor;
$thema = $row->thema;
$content = $row->content;
$datum = date("d.M.Y",$row->stamp);
$uhrzeit = date("H:i",$row->stamp);
echo"<h1>$thema <span>$autor  |   $datum   $uhrzeit</span></h1>";
echo nl2br ("<p>$content</p>");
echo"<p><a href="content_comment.php?bid=$bid">Kommentar</a></p>";
$qry = "SELECT COUNT(bid) FROM blog_comment WHERE bid = '$bid'";
if (($resC = mysql_query($qry)) && mysql_num_rows($resC) && ($rowC = mysql_fetch_object($resC)))
echo $row;
} // end while
} // end if
Hallo Andi,
also der Andi ist immer einen Tick schneller ;-)
Grüsse
Frankie
Hallo,
ja mit copy & paste :)
Gruß
Andi
Danke für deine Mühe und Unterstützung, habe es versucht nur kommt jetzt "Object" bei der echo ausgabe row.
Danke für deine Mühe und Unterstützung, habe es versucht nur kommt jetzt "Object" bei der echo ausgabe row.
Ach mein Fehler:
$qry = "SELECT COUNT(bid) AS bidC FROM blog_comment WHERE bid = '$bid'";
if (($resC = mysql_query($qry)) && mysql_num_rows($resC) && ($rowC = mysql_fetch_object($resC)))
echo $rowC->bidC;
Hallo Kai,
Danke für deine Mühe und Unterstützung, habe es versucht nur kommt jetzt "Object" bei der echo ausgabe row.
Hast Du eigetlich schon mal versucht selbst etwas nachzudenken? Verwende anstelle von $row die Variable, die Du ausgeben lassen möchtest.
Grüsse
Frankie
Hallo Kai,
Danke für eure schnellen Antworten, aber jetzt kommt folgende Fehlermeldung:
Warning: mysql_result(): Unable to jump to row 1 on MySQL result index 9 in /usr/export/www/vhosts/funnetwork/hosting/schildi/blog/content.php on line 6
[...]
Dein ganzes for()-Konstrukt solltest Du nochmals überdenken, insbesondere darüber nachdenken wann und wo $res und $i welche Werte zugewiesen werden. Als nützlich hat erfahrungsgemäss erwiesen, sich die Werte an geeigneter Stelle zur Kontrolle ausgeben zu lassen.
Grüsse
Frankie
Hallo Kai,
$res = mysql_query ("COUNT(bid) FROM blog_comment WHERE bid = '$bid' GROUP BY bid");
Naja, ich denke mal da fehlt noch ein SELECT, oder was meinst Du?
Grüsse
Frankie