The code Sarfraz posted is going to create invalid anchor tags (i.e. links). They'll also be missing names. Here is the shorter/faster version:
function get_menu($db)
{
$result = $db->rq('SELECT id,title FROM pages');
$menu = '';
while ($page = $db->fetch($result))
{
$id = $page['id'];
$title = $page['title'];
$menu .= "<a href='page.php?id={$id}&title={$title}'>{$title}</a>\n";
}
return $menu;
}
To use that do this:
echo get_menu($db);
The error you were getting was probably resulting from not passing the database connection to the function.
NOTE: It's generally not a good idea to show database ID numbers to the user in the interest of security; slugs are much better for identifying pages and are SEO friendly. Also, there shouldn't be any need to pass the page title to page.php because if you've got the ID you can get that when you need it from the database. Here's the code with this in mind:
function get_menu($db)
{
$result = $db->rq('SELECT id,title FROM pages');
$menu = '';
while ($page = $db->fetch($result))
{
$menu .= "<a href='page.php?id={$page['id']}'>{$page['title']}</a>\n";
}
return $menu;
}