MySQL: mysql_fetch_assoc()

Hallo Allerseits,

Ich habe ein kleines News-Script, welches auch mal funktionierte...ich bin schon ganz kirre, finde den Fehler einfach nicht.

Vielleicht könnt Ihr mir helfen:
PHP:
<?PHP
function displayNews($all = 0) {
	global $db, $max_items;
	if ($all == 0) {
		$query = "SELECT id,title,newstext,DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM news ORDER BY postdate DESC LIMIT ". $max_items;
	} else {
		$query = "SELECT id,title,newstext,DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM news ORDER BY postdate DESC";
	}
	$result = mysql_query ($query);
	while ($row = mysql_fetch_assoc ($result)) {
		echo "<TABLE border=\"1\" width=\"300\">";
		$date = $row['date'];
		$title = htmlentities ($row['title']);
		$news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
		echo "<TR><TD><b>".$title."</b> gepostet am ".$date."</TD></TR>\n";
		echo "<TR><TD>".$news."</TD></TR>\n";
		$comment_query = "SELECT count(*) FROM news_comments WHERE news_id=".$row['id'];
		$comment_result = mysql_query ($comment_query);
		$comment_row = mysql_fetch_row($comment_result);
		echo "<TR><TD><a href=\"".$_SERVER['PHP_SELF']."?action=show&id=".$row['id']."\">Comments</a>" .$comment_row[0]."</TD></TR>\n";
		echo "</TABLE>\n";
		echo "<BR>\n";
	}
	if ($all == 0) {
		echo "<a href='".$_SERVER['PHP_SELF'] ."?action=all'>Alle News</a>\n";
	}
}

function displayComments($id) {
	global $db;
	$query = "SELECT * FROM news_comments WHERE news_id=".$id."\"";
	$result = mysql_query ($query);
	echo "Kommentare:<BR><HR width=\"300\>\n";
	while ($row = mysql_fetch_assoc ($result)) {
		echo "<TABLE border=\"1\" width=\"300\">\n";
		$name = htmlentities ($row['name']);
		echo "<TR><TD><b>von: $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";
	}
	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=\"Kommentar hinzufügen\"\n";
	echo "</FORM>\n";
}

function displayOneItem($id) {
	global $db;
	$query = "SELECT * FROM news WHERE id=$id";
	$result = mysql_query ($query);
	if (mysql_num_rows ($result) == 0) {
		echo "Keine News idn";
		return;
	}
	$row = mysql_fetch_assoc($result);
	echo "<TABLE border=\"1\" width=\"300\">n";
	$title = htmlentities ($row['title']);
	$news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
	echo "<TR><TD><b>$title</b></TD></TR>\n";
	echo "<TR><TD>$news</TD></TR>\n";
	echo "</TABLE>\n";
	echo "<BR>\n";
	displayComments($id);
}

function addComment($id) {
	global $db;
	$query = "INSERT INTO news_comments VALUES('',".$id.",'".$_POST['name']."','".$_POST['comment']."')";
	mysql_query($query);
	echo "Danke!<BR>\n";
	echo "<a href=\"".$_SERVER['PHP_SELF']." ?action=show&id=".$id ."\">Zurück</a>\n";
}

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";
$max_items = 5;
$db = mysql_connect('localhost','user','passwort');
mysql_select_db ('test',$db);
?>

bei mir kommt nur noch die Fehlermeldung: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in Line 10

Vielen Dank im Voraus
 
Tja, dann stimmt offensichtlich irgendwas mit der Query nicht. Existieren wirklich alle Spalten? Heisst die Tabelle genau so? Überprüf alles nochmal genau, ansonsten häng mal ein or die(mysql_query()) an die Query an, das sollte Auskunft geben.
 
Hi SilentWarrior+Gumbo,
danke für die schnelle Antwort!

die gibt "Wrong parameter count for mysql_query() line10" aus, hier mal die Tables, ich kann da nix erkennen. :(

CREATE TABLE news (
id int(10) unsigned NOT NULL auto_increment,
postdate timestamp(14) NOT NULL,
title varchar(50) NOT NULL default '',
newstext text NOT NULL,
PRIMARY KEY (id),
KEY postdate (postdate)
) TYPE=MyISAM;

CREATE TABLE news_comments (
id int(10) unsigned NOT NULL auto_increment,
news_id int(10) unsigned NOT NULL default '0',
name varchar(40) NOT NULL default '',
COMMENT text NOT NULL,
PRIMARY KEY (id),
KEY news_id (news_id)
) TYPE=MyISAM;

@Gumbo: soweit ich mich erinnern kann, hab ich nichts gross ändern müssen, wo/wie schlagst du Umnennen vor?
 
Zuletzt bearbeitet:
Hi melmager!

wie meinst du "hinter limit fehlt eine zahl"?
hintendran wird doch 5 übergeben ($max_items).

Das mit "as datum" und $date = $row['datum'] funktioniert leider auch nicht :(

Dieses Script bringt mich noch ins Grab...
 
Hast Du schon mal versucht mit Hilfe eines echo's zu schauen, wie das Statement aussieht?
Vielleicht ergibt sich daraus ja schon die Lösung, ... (mir hilft sowas meistens weiter)!

redlama
 
Hi redlama,
das war ein Versuch wert, leider ohne Erfolg:
echo:
Code:
SELECT id,title,newstext,DATE_FORMAT(postdate, '%Y-%m-%d') as datum FROM news ORDER BY postdate DESC LIMIT 5
 
Hast Du zufällig phpMyAdmin? Dann könntest Du das Statement, welches Du mit echo bekommen hast, mal dort ausführen und schauen, ob Du überhaupt ein Ergebnis geliefert bekommst!

redlama
 

Neue Beiträge

Zurück