First thing I would say is you need to stick to a single way of doing braces, proper indentation etc. As such without changing the function or the code itself (but rather just the spacing) I would have it look like this (note Using PHP tags for highlighting, you should do this too on these boards). Note I also changed some variable names so they are more descriptive (and therefore more useful to you, the programmer) also removed the loops that don't make sense really (as BG pointed out).
$prev = "SELECT id, num FROM `myDB` WHERE `act` = 'Y' and id < $id ORDER BY id DESC LIMIT 1";
$next = "SELECT id, num FROM `myDB` WHERE `act` = 'Y' and id > $id ORDER BY id ASC LIMIT 1";
$resultPrev = mysql_query($prev) or die ("Couldn't execute query.");
if( mysql_num_rows($resultPrev) == 0 ) {
echo "<img src=\"images/endpage.png\" width=\"45\" height=\"45\" alt=\"Previous Listing\" class=\"pic2\"/>";
} else {
$rowPrev = mysql_fetch_assoc($resultPrev);
echo "<a href=\"http://www.mysite.com/test/".$rowPrev['num'].".html\"><img src=\"images/back12.png\" width=\"45\" height=\"45\" alt=\"Previous Listing\" class=\"pic2\"/></a>";
}
$resultNext = mysql_query($next) or die ("Couldn't execute query.");
if( mysql_num_rows($resultNext) == 0 ) {
echo " <div><img src=\"images/endpage.png\" width=\"45\" height=\"45\" alt=\"Next Listing\" class=\"pic3\" />";
} else {
$rowNext = mysql_fetch_assoc($resultNext);
echo " <div><a href=\"http://www.mysite.com/test/".$rowNext['num'].".html\"><img src=\"images/fwd12.png\" width=\"45\" height=\"45\" alt=\"Next Listing\" class=\"pic3\"/></a>";
}
Also you can delimit strings with single quotes so you don't have to escape double quotes within the string (AKA those echos). There is another thing that doesn't make sense as well: The fact that you use the 'num' column to build your link, but you based the query off of the id.
I wouldn't use query or die statements either, I prefer to check if the $result variable is false instead, something like this:
$qry = "SELECT 1 as num";
$res = mysql_query($qry);
if( $res === FALSE ) {
echo "Query failed: $qry<br>Error: ". mysql_error();
} elseif( mysql_num_rows($res) == 0 ) {
echo "No rows found.";
} else {
while( $row = mysql_fetch_assoc($res) ) {
echo $row['num'];
}
}
Also note, the entire mysql library has been deprecated in favor of mysqli (the i stands for improved) or PDO (PHP Data Objects). I only used it in my example so as not to throw more confusion on you, but you really NEED to use the proper library. See Choosing an API for more info.