Jump to content

[SOLVED] Warning: mysql_result(): supplied argument is not a valid MySQL result resource


Mr. R

Recommended Posts

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!

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();
  }

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.