Why do I get the following error? and How to fix?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in/home/gaogier/public_html/pages/monsters.php on line 217

If i add the or die part, I get this message.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

What do I do?

Thanks.

Here is my code

<?php
require_once ('includes/connect.php');//connect to db
function monster() {    

    $pages = $_GET['pages'];
    $order = $_GET['order'];
    $desc = $_GET['desc'];
    if(empty($order)){
        $order = "name";
    }
    if(empty($desc)){
        $desc = 0;
    }    
    if(empty($pages)){
        $pages = 1;
    }        

    if ($order == "name"){
        if ($desc == 1){
            $namelink = '<a href="/monsters/name/'.$pages.'/0">Name</a>';
            $a = "DESC";            
        } else {
            $a = "ASC";         
            $namelink = '<a href="/monsters/name/'.$pages.'/1">Name</a>';
        }
        $memberlink = '<a href="/monsters/members/'.$pages.'/0">Members?</a>';
        $lvllink = '<a href="/monsters/lvl/'.$pages.'/0">Level</a>';
        $hplink = '<a href="/monsters/hp/'.$pages.'/0">HP</a>';
    }   
    elseif ($order == "lvl"){
        if ($desc == 1){
            $a = "DESC";            
            $lvllink = '<a href="/monsters/lvl/'.$pages.'/0">Level</a>';
        } else {
            $a = "ASC";         
            $lvllink = '<a href="/monsters/lvl/'.$pages.'/1">Level</a>';
        }
        $namelink = '<a href="/monsters/name/'.$pages.'/0">Name</a>';
        $memberlink = '<a href="/monsters/members/'.$pages.'/0">Members?</a>';
        $hplink = '<a href="/monsters/hp/'.$pages.'/0">HP</a>';
    } elseif ($order == "members") {
        if ($desc == 1){
            $a = "DESC";
            $memberlink = '<a href="/monsters/members/'.$pages.'/0">Members?</a>';
        }else{
            $a = "ASC";
            $memberlink = '<a href="/monsters/members/'.$pages.'/1">Members?</a>';
        }
        $namelink = '<a href="/monsters/name/'.$pages.'/0">Name</a>';
        $lvllink = '<a href="/monsters/lvl/'.$pages.'/0">Level</a>';
        $hplink = '<a href="/monsters/hp/'.$pages.'/0">HP</a>';
    } elseif ($order == "hp") {
        if ($desc == 1){
            $a = "DESC";
            $hplink = '<a href="/monsters/hp/'.$pages.'/0">HP</a>';
        }else{
            $a = "ASC";
            $hplink = '<a href="/monsters/hp/'.$pages.'/1">HP</a>';
        }
        $namelink = '<a href="/monsters/name/'.$pages.'/0">Name</a>';
        $lvllink = '<a href="/monsters/lvl/'.$pages.'/0">Level</a>';
        $memberlink = '<a href="/monsters/members/'.$pages.'/0">Members?</a>';
    }

    $limit          = 25;               
    $query_count    = "SELECT id, name, lvl, members FROM monsters ORDER BY name ASC";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count);

    $limitvalue = $pages * $limit - ($limit);
    $query  = "SELECT id, name, lvl, hp, location, slayerxp, weakness, xp, members FROM monsters ORDER BY ".$order." ".$a." LIMIT $limitvalue, $limit";        
    $result = mysql_query($query_count);
    $result1 = mysql_query($query);

    if(mysql_num_rows($result) == 0){
        echo 'No monsters were found in the database';
    }

echo '<div id="contenttitle">Monster Database Beta</div>
        <div class="news">';  

    //echo $query;
    echo '<center>
            <p>&nbsp;</p>        
                <table width=615 cellSpacing=1 cellPadding=2 align=center border=1 style="border: 1px #857553 solid; border-collapse: collapse;" class="rgtb">
                    <tr style="border: 1px #857553 solid; background: #9a7435;" class="tableheader">
                        <td>'.$namelink.'</td>
                        <td>'.$lvllink.'</td>
                        <td>'.$hplink.'</td>
                        <td>Weakness</td>
                        <td>XP</td>
                        <td>Slayer XP</td>
                        <td>'.$memberlink.'</td>
                        <td>Location</td>
                    </tr>';
        $array = array('c4b189', 'd5c29a');
        $q = 0; 
    while($row = mysql_fetch_array($result1)){ 
        $count = $count + 1; 
        $name = $row['name'];        
        $level = $row['lvl'];
        $hp = $row['hp'];
        $xp = $row['xp'];
        $slayer = $row['slayerxp'];
        $location = $row['location'];
        $members = strtoupper($row['members']);
        if ($members == "Y") {
            $mem = "Yes";
        }else{
            $mem = "No";
        }      
        $weakness = strtoupper($row['weakness']);
        if ($weakness == "1") {
            $weak = "Melee - Stab";
        }elseif($weakness == "2") {
            $weak = "Melee - Crush";
        }elseif($weakness == "3") {
            $weak = "Melee - Slash";
        }elseif($weakness == "4") {
            $weak = "Ranged - Arrow";
        }elseif($weakness == "5") {
            $weak = "Ranged - Bolt";
        }elseif($weakness == "6") {
            $weak = "Ranged - Thrown";
        }elseif($weakness == "7") {
            $weak = "Mage - Earth Spells";
        }elseif($weakness == "8") {
            $weak = "Mage - Air Spells";
        }elseif($weakness == "9") {
            $weak = "Mage - Water Spells";
        }elseif($weakness == "10") {
            $weak = "Mage - Fire Spells";
        }elseif($weakness == "11") {
            $weak = "Unknown";
        }else{
            $weak = "Non-Specifc";
        }      
        echo '<tr style="background: #'.$array[$q].';">
                    <td>
                        <a href="/monsters/show/'.str_replace(' ','_',$name).'/'.$row['id'].'">'  . $name . '</a>
                    </td>
                    <td>'.$level.'</td>
                    <td>'.$hp.'</td>
                    <td>'.$weak.'</td>
                    <td>'.$xp.'</td>
                    <td>'.$slayer.'</td>
                    <td>'.$mem.'</td>
                    <td>'.$location.'</td>
        </tr>';
        if($q == 1) { $q = 0; } else { $q++; }
        if($count == 2){
            $count = 0;
        }   
    }

    echo '</table>';

    if($pages != 1){
        $pageprev = $pages-1;        
        echo '<a href="/monsters/'.$order.'/'.$pageprev.'/'.$desc.'">Previous</a>&nbsp;&nbsp;';
    }else{
        echo 'Previous&nbsp;&nbsp;';
    }

    $numofpages = $totalrows / $limit;

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $pages){
            echo $i.' ';
        }else{
            echo '&nbsp;<a style=color:#EED8B4;  href="/monsters/'.$order.'/'.$i.'/'.$desc.'">'.$i.'</a>&nbsp;';
        }
    }
    if(($totalrows % $limit) != 0){
        if($i == $pages){
            echo $i.' ';
        }else{
            echo '&nbsp;<a href="/monsters/'.$order.'/'.$i.'/'.$desc.'">'.$i.'</a>&nbsp;';
        }
    }
    if(($totalrows - ($limit * $pages)) > 0){
        $pagenext = $pages+1;

        echo '&nbsp;&nbsp;<a style=color:#EED8B4; href="/monsters/'.$order.'/'.$pagenext.'/'.$desc.'">Next</a>';
    }else{
        echo '&nbsp;&nbsp;Next';
    }

    mysql_free_result($result);
    echo '</center>';
echo 'There are currently '.$totalrows.' monsters in our database. Help us grow by submitting monster information/ammendments <a style=color:#EED8B4; href="submit.php?type=monster">here</a><br /><br />'; 

}
function processRow($count) {
    switch($count){
           case 1:
                $color = "#0B0B0B";
                break;
           case 2:
                $color = "#101010";
                break;
           default:
                $color = "#0B0B0B";
}
return $color;
}

function show($id) {
$id = explode('/',$id);
$id=$id[1];

    /* query for item */
    $query = "SELECT * FROM monsters WHERE id=$id";
    $result = mysql_query ($query);

    /* if we get no results back, error out */
$numrtn = mysql_num_rows($result);

    if ($numrtn == 0) {
        echo "The monster requested cannot be found\n";
        return;
    }
    $row = mysql_fetch_assoc($result);
    /* easier to read variables and 
     * striping out tags */
    $name = $row['name'];
    $lvl = $row['lvl'];
    $hp = $row['hp'];
    $xp = $row['xp'];
    $race = $row['race'];
    $location = $row['location'];
    $members = $row['members'];
    $examine = $row['examine'];
    $contrib = $row['contributedby'];
    $drops = $row['drops'];
    $info = $row['info'];
    $picture = $row['picture'];
    $sxp = $row['slayerxp'];
    if ($members == "Y") {
    $mem = "Yes";
    }else{
    $mem = "No";
    }
    $xphp1 = $hp * 1;
    $xpo = $hp * 0.4; 
    $xp1 = $xpo / 3 * 4;

    $xphp = $xpo / 3;
    $xp = sprintf('%.0f', $xp1);

    /* display the items */   
echo '<div id="contenttitle">Monster Database Beta</div>
        <div class="news"><br><br>
    <center><table border="0.5" cellspacing="1" cellpadding="2" width="615" align="center" bgcolor="#857553">

        <tr align="middle" bgcolor="#9a7435">
          <td colspan=6><center><span style="color: black; font-size: small; font-weight: normal;"><b>'.$name.'</b></span></center></td>
        </tr>
    <tr align="middle" bgcolor="#c4b189">

          <td><span style="color: black; font-size: small; font-weight: normal;">Picture:</span></td>
          <td colspan=6><img src="/monster/'.$picture.'" /></td>
    </tr>
    <tr align="middle" bgcolor="#d5c29a">
      <td height="23" width="19%"><span style="color: black; font-size: small; font-weight: normal;">Location:</span></td>
          <td colspan=6><span style="color: black; font-size: small; font-weight: normal;">'.$location.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#c4b189">
          <td height="23" width="24%"><span style="color: black; font-size: small; font-weight: normal;">Race:</span></td>
          <td><span style="color: black; font-size: small; font-weight: normal;">'.$race.'</span></td>
          <td width="24%"><span style="color: black; font-size: small; font-weight: normal;">Slayer XP:</span></td>
          <td colspan=2><span style="color: black; font-size: small; font-weight: normal;">'.$sxp.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#d5c29a">
          <td height="23"><span style="color: black; font-size: small; font-weight: normal;">Hitpoints:</span></td>
          <td><span style="color: black; font-size: small; font-weight: normal;">'.$hp.'</td>
          <td><span style="color: black; font-size: small; font-weight: normal;">Xp gained total:</span></td>
          <td colspan=2><span style="color: black; font-size: small; font-weight: normal;">'.$xp.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#c4b189">
          <td height="23"><span style="color: black; font-size: small; font-weight: normal;">Xp gained to hits:</span></td>
          <td><span style="color: black; font-size: small; font-weight: normal;">'.$xphp.'</span></td>
          <td width="24%"><span style="color: black; font-size: small; font-weight: normal;">Xp that goes to other combat skill:</span></td>
          <td colspan=2><span style="color: black; font-size: small; font-weight: normal;">'.$xpo.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#d5c29a">
          <td><span style="color: black; font-size: small; font-weight: normal;">Level:</span></td>
          <td><span style="color: black; font-size: small; font-weight: normal;">'.$lvl.'</span></td>
          <td><span style="color: black; font-size: small; font-weight: normal;">Members?</span></td>
          <td colspan=2><span style="color: black; font-size: small; font-weight: normal;">'.$mem.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#c4b189">
          <td><span style="color: black; font-size: small; font-weight: normal;">Examine:</span></td>
          <td colspan=6><span style="color: black; font-size: small; font-weight: normal;">'.$examine.'</span></td>
    </tr>
    <tr align="middle" bgcolor="#d5c29a">
          <td><span style="color: black; font-size: small; font-weight: normal;">Contributors:</span></td>
          <td colspan=6><span style="color: black; font-size: small; font-weight: normal;">'.$contrib.'</span></td>
    </tr>
    <tr align="middle" bgcolor="#c4b189">
          <td height="74" width="24%"><span style="color: black; font-size: small; font-weight: normal;">Loot:</span></td>
          <td colspan=6><span style="color: black; font-size: small; font-weight: normal;">'.$drops.'</span></td>
        </tr>
    <tr align="middle" bgcolor="#d5c29a">
          <td height="74" width="24%"><span style="color: black; font-size: small; font-weight: normal;">Info:</span></td>
          <td colspan=6><span style="color: black; font-size: small; font-weight: normal;">'.$info.'</span></td>
        </tr>
</table>'; include("includes/social.php");       

}





/* this is where the script decides what do do */
switch($_GET['action']) {

    case 'show':
        show($_GET['id']);
        break;
    case 'all':
        monster();
        break;
    default:
        monster();
}
echo '<br /></div>';

?>

You can try:

if(!mysql_num_rows($result) || mysql_num_rows($result) == 0) 
{  
    echo "<b>Not Found</b>";  
}

or

 (mysql_num_rows($result) < 1)

If that still doesnt work, then the problem is before that, with your select statement.

Missing quotes:

$query = "SELECT * FROM monsters WHERE id='$id'"

If that doesn't work, var_dump your $id and make sure it's being set properly

You can do without the quotes if id is an integer column. I think the query fails because $id does not have a value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.