d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help Fixing A Mysql_num_rows() Error
Add Reply New Topic New Poll
Member
Posts: 775
Joined: Jun 8 2011
Gold: 154.60
Dec 17 2012 08:08pm
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



Here is my code (if i am allowed to post a pastebin link could a mod tell me as its a lot of text as the rules said no links)
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>';

?>


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

This post was edited by Mac_Daddy on Dec 17 2012 08:12pm
Member
Posts: 624
Joined: May 20 2007
Gold: 17.33
Dec 26 2012 02:25pm
Most of the times this happens, when the syntax of your query isnt right.
if the syntax is wrong(error below), your result wont be a valid result ressource (error above).

i did not check your code (too much), but since the query is built dynamically, i guess the error comes from a not-set variable.
try to echo your query and if you understand mysql syntax you will find the error.

as i said i didnt check your code very much, but i guess the error is here:
Code
if(empty($order))


try
Code
print_r(get_defined_vars());

to print out all your variables.
hope this helps.
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Dec 26 2012 03:21pm
YOU ARE DEFINING $query TWICE LOL

ON 213 and 71
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll