getting the problem ~
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /studhome/1/0803087/public_html/RSS.php on line 14

trying to create a RSS feed
no idea what is wrong, so can anyone help

<?
// Connect to database
DEFINE ('DB_USER', '******'); 
DEFINE ('DB_PASSWORD', '********'); 
DEFINE ('DB_HOST', '*****'); 
DEFINE ('DB_NAME', '******');

// Get latest items from db
$sql = "SELECT Name FROM Games ORDER BY date_added DESC limit 0,10";
$rst = mysql_query($sql);

// Loop through data and build feed items
$items = "";
while($a_row = mysql_fetch_assoc($rst))
{
$items.= "<item>";
$items.= " <title>{$a_row['title']}</title>";
$items.= " <link>{$a_row['link']}</link>";
$items.= " <description>{$rst['description']}</description>";
$items.= "</item>"; 
}

// Build feed text
$feed = "<?xml version=\"1.0\"?>";
$feed.= "<rss version=\"2.0\">";
$feed.= " <channel>";
$feed.= " <title>My cool feed</title>";
$feed.= " <link>http://www.dude.com/</link>";
$feed.= " <description>A cool feed</description>";
$feed.= " $items";
$feed.= " </channel>";
$feed.= "</rss>";

// Display feed text
echo $feed;
?>

Dani AI

Generated

The runtime message means the fetch function received something other than a valid result resource because the prior query failed — mysql_query() returns a result resource on success and false on error, so calling mysql_fetch_assoc() on that false value triggers this warning. Common causes are a missing/failed DB connection, SQL syntax errors, wrong table/column names, or permission problems. Always check the query return value before fetching and print the DB error string to see why it failed. See the PHP docs for how mysql_query() behaves and how to obtain the MySQL error message. mysql_query() manual mysql_error() manual. (php.net)

As pointed out, forgetting to open a connection is the most common cause — and confirmed adding the connection fixed the immediate problem. Beyond that, check two logic slips that often appear in feed generators: the SQL must return the exact columns you later reference (title/link/description), and you must read those fields from the fetched row (the associative array) — not from the result handle. If a query returns false, stop and report the DB error rather than entering the fetch loop.

For long-term compatibility and security, stop using the old mysql_* extension (it was deprecated and later removed). Move to mysqli or PDO and use prepared statements to prevent injection and to work on modern PHP builds. Below is a minimal PDO pattern (connection, query, fetch, XML-escape values) you can adapt:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','user','pass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$stmt = $pdo->query('SELECT title, link, description FROM Games ORDER BY date_added DESC LIMIT 10');
while ($row = $stmt->fetch()) {
  echo '<item>';
  echo '<title>' . htmlspecialchars($row['title'], ENT_XML1, 'UTF-8') . '</title>';
  echo '<link>' . htmlspecialchars($row['link'], ENT_XML1, 'UTF-8') . '</link>';
  echo '<description>' . htmlspecialchars($row['description'], ENT_XML1, 'UTF-8') . '</description>';
  echo '</item>';
}
?>

The original mysql extension is deprecated/removed; prefer mysqli or PDO and use prepared statements. See the PHP docs for details on the extension removal and on prepared statements in mysqli and PDO. MySQL extension intro / deprecation note mysqli prepared statements PDO::prepare.

Quick extra tips: send the RSS Content-Type header, escape XML properly (e.g. htmlspecialchars() with ENT_XML1), consider DOMDocument for robust XML generation, and test failing SQL directly in your DB client while enabling error reporting during development.

Recommended Answers

All 2 Replies

You have to establish a connection first with mysql_connect().
Try

$rst = mysql_query($sql) or die(mysql_error());

to learn more about the problem.

Thanks! Duhhh that helped, i actually put that in, then took it out for some reason. Amateur mistake :yawn:

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.