Author Topic: Explore.php  (Read 656 times)

Offline Warlords

  • Level 4
  • *
  • Posts: 10
  • Reputation: +0/-0
    • View Profile
Explore.php
« on: November 01, 2010, 01:40:31 PM »
Greetings,

I got these errors.

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/a9491872/public_html/explore.php on line 133



Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/a9491872/public_html/explore.php on line 144


Quote
<?php
 
require_once 'smarty.php';
require_once 'weapon-stats.php';
require_once 'login-check.php';
 
session_start();
 
require_once 'config.php';      // our database settings
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
   or die('Error connecting to mysql');
mysql_select_db($dbname);
 
if($_POST) {
   if($_POST['action'] == 'Attack') {
      require_once 'stats.php';         // player stats
      require_once 'monster-stats.php';   // monster stats
      // to begin with, we'll retrieve our player and our monster stats
      $query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
               mysql_real_escape_string($_SESSION['username']));
      $result = mysql_query($query);
      list($userID) = mysql_fetch_row($result);
      $player = array (
         name      =>   $_SESSION['username'],
         attack       =>   getStat('atk',$userID),
         defence      =>   getStat('def',$userID),
         curhp      =>   getStat('curhp',$userID)
      );
      $phand = getStat('phand',$userID);
      $atk = getWeaponStat('atk',$phand);
      $player['attack'] += $atk;
      require_once 'armor-stats.php';      // armor stats
      $armor = array('atorso','ahead','alegs','aright','aleft');
      foreach ($armor as $key) {
         $id = getStat($key,$userID);
         $defence = getArmorStat('defence',$id);
         $player['defence'] += $defence;
      }      
 
      $query = sprintf("SELECT id FROM monsters WHERE name = '%s'",
               mysql_real_escape_string($_POST['monster']));
      $result = mysql_query($query);
      list($monsterID) = mysql_fetch_row($result);
      $monster = array (
         name      =>   $_POST['monster'],
         attack      =>   getMonsterStat('atk',$monsterID),
         defence      =>   getMonsterStat('def',$monsterID),
         curhp      =>   getMonsterStat('maxhp',$monsterID)
      );
      $combat = array();
      $turns = 0;      
      while($player['curhp'] > 0 && $monster['curhp'] > 0 && $turns <= 100) {
         if($turns % 2 != 0) {
            $attacker = &$monster;
            $defender = &$player;   
         } else {
            $attacker = &$player;
            $defender = &$monster;
         }
         $damage = 0;
         if($attacker['attack'] > $defender['defence']) {
            $damage = $attacker['attack'] - $defender['defence'];   
         }
         $defender['curhp'] -= $damage;
         $combat[$turns] = array(
            attacker   =>   $attacker['name'],
            defender   =>   $defender['name'],
            damage      =>   $damage
         );
         $turns++;
      }
      setStat('curhp',$userID,$player['curhp']);
      if($player['curhp'] > 0) {
         // player won
         setStat('gc',$userID,getStat('gc',$userID)+getMonsterStat('gc',$monsterID));   
         $smarty->assign('won',1);
         $smarty->assign('gold',getMonsterStat('gc',$monsterID));
         $rand = rand(0,100);
         $query = sprintf("SELECT item_id FROM monster_items WHERE monster_id = %s AND rarity >= %s ORDER BY RAND() LIMIT 1",
            mysql_real_escape_string($monsterID),
            mysql_real_escape_string($rand));
         $result = mysql_query($query);
         list($itemID) = mysql_fetch_row($result);
         $query = sprintf("SELECT count(id) FROM user_items WHERE user_id = '%s' AND item_id = '%s'",
            mysql_real_escape_string($userID),mysql_real_escape_string($itemID));
         $result = mysql_query($query);
         list($count) = mysql_fetch_row($result);
         if ($count > 0) {
            # already has one of the item
            $query = sprintf("UPDATE user_items SET quantity = quantity + 1 WHERE user_id = '%s' AND item_id = '%s'",
               mysql_real_escape_string($userID),
               mysql_real_escape_string($itemID));
         } else {
            # has none - new row
            $query = sprintf("INSERT INTO user_items(quantity,user_id,item_id) VALUES (1,'%s','%s')",
               mysql_real_escape_string($userID),
               mysql_real_escape_string($itemID));
         }
         mysql_query($query);
         # retrieve the item name, so that we can display it
         $query = sprintf("SELECT name FROM items WHERE id = %s",
            mysql_real_escape_string($itemID));
         $result = mysql_query($query);
         list($itemName) = mysql_fetch_row($result);
         $smarty->assign('item',$itemName);
 
         $monster_exp = getMonsterStat('exp',$monsterID);
         $smarty->assign('exp',$monster_exp);
         $exp_rem = getStat('exp_rem',$userID);
         $exp_rem -= $monster_exp;
         $level_up = 0;
         if($exp_rem <= 0) {
             // level up!
             $exp_rem = 100;
             $level_up = 1;
         }
          $smarty->assign('level_up',$level_up);
         setStat('exp_rem',$userID,$exp_rem);
      } else {
         // monster won
         $smarty->assign('lost',1);   
      }
      $smarty->assign('combat',$combat);
   } else {
      // Running away! Send them back to the main page
      header('Location: character.php');   
   }
} else {
    $area_id = $_GET['area'];
    $query = sprintf("SELECT monster FROM area_monsters WHERE area = %s ORDER BY RAND() LIMIT 1",
        mysql_real_escape_string($area_id));
    $result = mysql_query($query);
   list($monster_id) = mysql_fetch_row($result);
    $query = sprintf("SELECT name FROM monsters WHERE id = %s",
        mysql_real_escape_string($monster_id));
    $result = mysql_query($query);
    list($monster) = mysql_fetch_row($result);
    $smarty->assign('monster',$monster);
}
 
$query = sprintf("SELECT name FROM areas WHERE id = %s",
    mysql_real_escape_string($_GET['area']));
$result = mysql_query($query);
list($area_name) = mysql_fetch_row($result);
$smarty->assign('area',$area_name);
$smarty->assign('area_id',$_GET['area']);
 
$smarty->display('explore.tpl');
 
?>

Does anyone have a clue?

Error's are show in bold.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Explore.php
« Reply #1 on: November 01, 2010, 01:58:43 PM »
1) You do not test whether you got any data (perhaps the table is empty) from the query or not
If yes, then:
2) When in this kind of trouble print the query and very often you see the mistake
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Explore.php
« Reply #2 on: November 01, 2010, 01:59:01 PM »
*shudders*

There are a few things here, but I'll stick to the question.

SELECT monster FROM area_monsters WHERE area = %s ORDER BY RAND() LIMIT 1",
        mysql_real_escape_string($area_id)

From this statement, it appears you're selecting more than 1 row with your query statement.  I also believe you're skipping the first row of the returned data.  This does not appear to be what you've intended.

You may want to try replacing

mysql_query($query);

with

mysql_query($query) or die(mysql_error());

You also may want to try doing:

$Result = mysql_query($query)
echo mysql_num_rows($Result);

to see how many rows your array is returning.  If zero (ie, valid query, no data), then your mysql_fetch_row() statement won't work (ie, it will throw the error you're describing).



Offline Warlords

  • Level 4
  • *
  • Posts: 10
  • Reputation: +0/-0
    • View Profile
Re: Explore.php
« Reply #3 on: November 01, 2010, 02:23:11 PM »
Quote
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 'ORDER BY RAND() LIMIT 1' at line 1

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Explore.php
« Reply #4 on: November 01, 2010, 02:26:16 PM »
Syntax errors are something that one should be able to fight alone, but...of course we'll help, but then at least give us more info in your posts,
please post the query as it obviously contains some error
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Warlords

  • Level 4
  • *
  • Posts: 10
  • Reputation: +0/-0
    • View Profile
Re: Explore.php
« Reply #5 on: November 01, 2010, 02:34:21 PM »
i just solves this problem:P but thanks for the help anyways.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal