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! Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/ 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! Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-339824 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-339836 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); Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-339839 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. Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-339841 Share on other sites More sharing options...
Mr. R Posted September 2, 2007 Author Share Posted September 2, 2007 Cheers guys, its working now! Link to comment https://forums.phpfreaks.com/topic/67648-solved-warning-mysql_result-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-339865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.