<?php

require("config.php");

if(isset($_GET['id']) == TRUE) {
if(is_numeric($_GET['id']) == FALSE) {
$error = 1;
}

if($error == 1) {
header("Location: " . $config_basedir);
}
else {
$validtopic = $_GET['id'];
}
}
else {
header("Location: " . $config_basedir);
}

require("header.php");

$topicsql = "SELECT topics.subject, topics.forum_id, forums.name FROM topics, forums WHERE topics.forum_id = forums.id AND topics.id = " . $validtopic . ";";
$topicresult = mysql_query($topicsql);
$topicrow = mysql_fetch_assoc($topicresult);

echo "<h2>" . $topicrow['subject'] . "</h2>";
echo "<a href='index.php'>". $config_forumsname . " forums</a> -> <a href='viewforum.php?id=" . $topicrow['forum_id'] . "'>" . $topicrow['name'] . "</a><br /><br />";

$threadsql = "SELECT messages.subject, messages.body, messages.date, users.username FROM messages, users WHERE messages.user_id = users.id AND messages.topic_id = " . $validtopic . " ORDER BY messages.date;";
$threadresult = mysql_query($threadsql);

echo "<table>";

while($threadrow = mysql_fetch_assoc($threadresult))
{
echo "<tr><td><strong>Posted by <i>" . $threadrow['username'] . "</i> on " . date("D jS F Y g.iA", strtotime($threadrow['date'])) . " - <i>" . $threadrow['subject'] . "</i></strong></td></tr>";
echo "<tr><td>" . $threadrow['body'] . "</td></tr>";
echo "<tr></tr>";
}

echo "<tr><td>[<a href='reply.php?id=" . $validtopic . "'>reply</a>]</td></tr>";
echo "</table>";

require("footer.php");

?>

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/harlow/public_html/forums/viewmessages.php on line 35

That's the warning and my code, I've looked over and over and can't find the problem. I'm relatively new to PHP, so I may be overlooking something.

Could anyone give me some pointers?
Thank you!

Dani AI

Generated

Short answer: that fetch error means the fetch function got FALSE instead of a result resource because the preceding query failed (or no DB connection was active). Common causes are a broken SQL statement (empty or malformed WHERE), a missing/failed DB connection, or the script continuing after a redirect so an expected variable is unset.

Practical checks and fixes:

  • Validate and canonicalize the ID right away, and stop the script if it is invalid. Example:

    $topicId = (isset($_GET['id']) && ctype_digit($_GET['id'])) ? (int)$_GET['id'] : 0;
    if ($topicId <= 0) {
        header('Location: ' . $config_basedir);
        exit;
    }

    This avoids building queries with an empty value.

  • Always check the query result before fetching, and show/log the DB error while debugging:

    $res = mysql_query($sql);
    if ($res === false) {
        error_log('MySQL error: ' . mysql_error() . ' SQL: ' . $sql);
        die('Database error (see log).');
    }

    Do not fetch unless the result is valid. In production, log details rather than die().

  • Ensure config.php actually connects and selects the DB and that connection errors are handled. Also add an exit; after any header('Location: ...') so execution stops instead of continuing with undefined variables.

  • Prefer mysqli or PDO with prepared statements (example below) to avoid SQL injection and to work on modern PHP:

    $mysqli = new mysqli($host,$user,$pass,$db);
    $stmt = $mysqli->prepare("SELECT t.subject, f.name FROM topics t JOIN forums f ON t.forum_id=f.id WHERE t.id=?");
    $stmt->bind_param('i', $topicId);
    $stmt->execute();
    $stmt->bind_result($subject, $forumName);
    $stmt->fetch();

Notes for thread: was right that the FAQ usually points to checking the query result, but here a quick, concrete debug (validate ID, stop after redirects, check mysql_error()) will show the exact reason. , try the checks above and post the DB error string if you still need help.

Recommended Answers

All 3 Replies

Read the FAQ that is right above your post on the PHP forum

I already read it, but it didn't really help me.

I already read it, but it didn't really help me.

Not true, read it again. It answers your question exactly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.