A Mysql_Num_Rows() Error

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?

Here is my code
[php]<?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 '

Monster Database Beta

';
//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 here

’;

}
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 '

Monster Database Beta




  	<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>
'; 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 ‘

’;

?>[/php]

I personlly first would shorten this line
[php]$query = “SELECT id, name, lvl, hp, location, slayerxp, weakness, xp, members FROM monsters ORDER BY “.$order.” “.$a.” LIMIT $limitvalue, $limit”; [/php]

to

[php]$query = “SELECT * FROM monsters ORDER BY $order $a LIMIT $limitvalue, $limit”;[/php]

get in the habit of using sigle quotes for string vars and “backquotes” for fields so the app know how to differentiate between fields and vars… this might work better.

The “*” just selects everything, and gets rid of extra typing since you are pulling up stats by assoc (or array).

Try that first. and see if it works.

also in your error trap change:
[php]$result1 = mysql_query($query);[/php]

to
[php]to $result1 = mysql_query($query) or die( “bad query
$query
”.mysql_error());[/php]

this is what i use. does 2 things, 1 shows the string (in your case $query) with all the vars filled in, and it will show you what it looks like, and you might see the error, and the error() will show you what sql was looking for in the string - with those two might help pin point it faster, in stead of the generic error that you get.

good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service