Mr. R Posted September 2, 2007 Share Posted September 2, 2007 Hi, iv just used the pagination script from this site, but i get an error.. Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\blaaah\adforms.php on line 38 This is the code.. <?php include 'connect.php'; if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $max_results = 10; $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $sql = mysql_query("SELECT * FROM advertising LIMIT $from, $max_results"); while($row = mysql_fetch_array($sql)){ echo $row['username']."<br />"; echo $row['email']."<br />"; echo $row['website']."<br />"; echo $row['shortd']."<br /><br /><br />"; } // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0); // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks echo "<center>Select a Page<br />"; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; } echo "</center>"; ?> and line 38 is $result = mysql_query("SELECT rank FROM users WHERE username = '$username'"); Please help, Thanks! Quote Link to comment Share on other sites More sharing options...
Mr. R Posted September 2, 2007 Author Share Posted September 2, 2007 sorry line 38 is... $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0); made a mistake. Please help now! Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 This usually meens your query failed. Nesting function calls as you have on that line prohibits your from any error handling and is always a bad idea. Try... <?php if ($result = mysql_query("SELECT COUNT(*) as Num FROM pages")) { $total_results = mysql_result($result,0); } else { echo mysql_error(); } ?> Quote Link to comment Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 U can go for another approach: $query = mysql_query("SELECT id FROM pages"); $totalResults = mysql_num_rows($query); Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 Even that should be done... <?php if ($result = mysql_query("SELECT id FROM pages")) { if (mysql_num_rows($result)) { $totalResults = mysql_num_rows($result); } } ?> to make sure you trap all errors. PS: $query is just a poor choice of variable name. Quote Link to comment Share on other sites More sharing options...
Mr. R Posted September 2, 2007 Author Share Posted September 2, 2007 Cheers guys, its working now! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.