Problem with attack.php....

Location of the Videos
Post Reply
lunaticthrasher
Posts: 2
Joined: Thu Oct 23, 2014 4:26 am

Problem with attack.php....

Post by lunaticthrasher »

Hello, I am new here but i was testing this out, trying to get a hang of something like this. Well i was updating the msql to mysqli of what i could and i have this problem in attack.php which i can't figure out. Here is what i have done,

Code: Select all

//////////////////////first function
function creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature)
{
	$isgold = rand(0,$creaturegold);
	$isloot = rand(0,100);

	echo "You loot " . $isgold . " gold pieces.<br>";

	$updateplayer="update players set gold=gold+'$isgold' where name='$player'";
>>line 302>>  mysqli_query($db,$updateplayer) or die("Could not update player");
  
	if($isloot < $hasloot)
	{
	echo "You find a " . $creatureloot . " on the " . $creature . ".<br>";
	/////////////////pull the items info
	 $critem="SELECT * from store where name='$creatureloot'";
   $critem2=mysqli_query($db,$critem) or die("Could not get creature item");
   $critem3=mysqli_fetch_array($critem2);
	/////////////////give it to player
	$randid4 = rand(999,99999999);
	$itembought = "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES 				('$pid','$critem3[name]','$critem3[stats]','$critem3[statadd]','$randid4','$critem3[type]')";
	mysqli_query($db,$itembought) or die("could not insert item into backpack");

	}
	else 
	{
	echo " You find no items of value on the creature.<br>";
this is the error I am getting,

Code: Select all

( ! ) Notice: Undefined variable: db in C:\wamp\www\tutorial\attack.php on line 302
Call Stack
#	Time	Memory	Function	Location
1	0.0008	306120	{main}( )	..\attack.php:0
2	0.0054	349536	creaturekilled( )	..\attack.php:184

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\tutorial\attack.php on line 302
Call Stack
#	Time	Memory	Function	Location
1	0.0008	306120	{main}( )	..\attack.php:0
2	0.0054	349536	creaturekilled( )	..\attack.php:184
3	0.0199	350104	mysqli_query ( )	..\attack.php:302
Could not update player
and here is the code from line 184,

Code: Select all

//////////////////
  creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature,$newcreaturehp);
echo $hasloot;
/////////////////////
        if ($playerinfo3['level'] > $creatureinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] - $thirdmod;
        }
        else
        {
         $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] + $thirdmod;
        }
          $totalexper = (int)$totalexper;

           echo "<br><b><big>You gain " . $totalexper . " experience.</b></big><br>";
      $updateplayer="update players set exper=exper+'$totalexper',creature=0 where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");



      echo "<a href='battle.php'>Go Back";
      exit;
   }

  $updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
  mysqli_query($db,$updatecreature) or die("Could not update creature");
}
else
{
  echo  $playerinfo3['name'] . " " . $weaponinfo3['hittext'] . " and misses!"; 
}}
?>
so what i am thinking since my problem is in a function it acts different then just regular query's?
Any help to resolve this would be appreciated, i am stumped. I haven't done anything yet except to try and get all the mysql problems out of the way first and this is my last one that i can see.

here is my complete attack.php,

Code: Select all

<?php
include_once 'connect.php';
  session_start();
  include_once 'logo.php';
?>
  
  <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">

<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
</div>
<?php
 $playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysqli_query($db,$playerinfo) or die("could not get player stats!");
$playerinfo3=mysqli_fetch_array($playerinfo2);
 include_once 'statpanel.php';
$pid = $playerinfo3['id'];
 ?>
</div>
<div id="table">
<?php

 $creature = $playerinfo3['creature'];
 if ($creature != 0)
{
 $creatureinfo="SELECT * from creatures where id='$creature'";
$creatureinfo2=mysqli_query($db,$creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysqli_fetch_array($creatureinfo2);

}
else
{
  echo "<a href='battle.php'>No Creature selected. Go Back!";
  exit;
}

$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];
$playerpass = 0;

$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];
$creaturegold = $creatureinfo3['gold'];
$creatureloot = $creatureinfo3['loot'];
$hasloot = $creatureinfo3['isloot'];

 ?>
 </div>

 <div id="player">
 <?php
 ///////////////////////players turn////////////////////
  
 echo "<center><u> " . $playerinfo3['name'] . "'s Attack</u><br>";
 if (isset($_GET['sid']))
{
   $sid=$_GET['sid'];
   $spellinfo="SELECT * from playermagic where sid='$sid' AND pid='$pid'";
 $spellinfo2=mysqli_query( $db,$spellinfo) or die("could not get spell!");
 $spellinfo3=mysqli_fetch_array( $spellinfo2);
 $sname = $spellinfo3['name'];
if ($spellinfo3['type'] == "combat")
	{
	if ($spellinfo3['scost'] > $playerinfo3['spoints'])
	{
	echo "You do not have enough Spell Points for this spell.<br>";
	 echo "<a href='battle.php'>Go Back";
	exit;
	}
	else
	{
	$spoints = $spellinfo3['scost'];
	$moddamage = $spellinfo3['svalue'] / 10;
	$moddamage = (int)$moddamage;
	$sdamage =  $spellinfo3['svalue'];
	$randdamage = rand(0, $sdamage);
	$sdamage = $randdamage + $moddamage; 
	
	echo " You Cast " . $sname . " and do " . $sdamage . " points of damage<br>";
	$playerpass = 1;
	 $newcreaturehp = $creaturehp - $sdamage;
	  $updateplayer="update players set spoints=spoints-'$spoints' where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");
  
  $updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
  mysqli_query($db,$updatecreature) or die("Could not update creature");
  
   if ($newcreaturehp < 1)
   {
     echo "The " . $creature . " has been killed<br>";

  $crmaxhp=$creatureinfo3['maxhpoints'];
      $updatecreature="update creatures set hpoints='$crmaxhp' where name='$creature' limit 1";
  mysqli_query($db,$updatecreature) or die("Could not update creature");
////////////////////////
//////////////////////
//////////////////
  creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature,$newcreaturehp);
//echo $hasloot;
/////////////////////
        if ($playerinfo3['level'] > $creatureinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] - $thirdmod;
        }
        else
        {
         $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] + $thirdmod;
        }
          $totalexper = (int)$totalexper;

           echo "<br><b><big>You gain " . $totalexper . " experience.</b></big><br>";
      $updateplayer="update players set exper=exper+'$totalexper',creature=0 where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");



      echo "<a href='battle.php'>Go Back";
      exit;
   }
  
  
  
	}} 
} 




if ($playerpass != 1)
{

 $weaponinfo="SELECT * from playerweapons where equip=1 AND pid='$pid'";
$weaponinfo2=mysqli_query($db, $weaponinfo) or die("could not get player weapon!");
$weaponinfo3=mysqli_fetch_array( $weaponinfo2);

$playerattack = rand(1,20) + $playerattack + $weaponinfo3['rating'];
//////////////////////////////////////////////////////
$creaturedefense = rand(1,20) + $creaturedefense;

echo $playerinfo3['name'] . "'s Attack roll is " . $playerattack . "<br>";
echo $creature . "'s defense roll is " . $creaturedefense. "<br>";

if ($playerattack  > $creaturedefense)
{
  echo  $playerinfo3['name'] . " " . $weaponinfo3['hittext'] . " and hits!<br>"; 
  
 
  $weapdamage = $weaponinfo3['damage'];
  $halfdamage = $weaponinfo3['damage'] / 2;
  $playerdamage = rand($halfdamage,$weapdamage);
   $newcreaturehp = $creaturehp - $playerdamage;
  echo "For " . $playerdamage . " points of damage. <br>";
   if ($newcreaturehp < 1)
   {
     echo "The " . $creature . " has been killed";

   $crmaxhp=$creatureinfo3['maxhpoints'];
       $updatecreature="update creatures set hpoints='$crmaxhp' where name='$creature' limit 1";
   mysqli_query($db,$updatecreature) or die("Could not update creature");
////////////////////////
//////////////////////
//////////////////
  creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature,$newcreaturehp);
echo $hasloot;
/////////////////////
        if ($playerinfo3['level'] > $creatureinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] - $thirdmod;
        }
        else
        {
         $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] + $thirdmod;
        }
          $totalexper = (int)$totalexper;

           echo "<br><b><big>You gain " . $totalexper . " experience.</b></big><br>";
      $updateplayer="update players set exper=exper+'$totalexper',creature=0 where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");



      echo "<a href='battle.php'>Go Back";
      exit;
   }

  $updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
  mysqli_query($db,$updatecreature) or die("Could not update creature");
}
else
{
  echo  $playerinfo3['name'] . " " . $weaponinfo3['hittext'] . " and misses!"; 
}}
?>
</div>
<div id="creature">
<?php
 //////////////////////creatures turn //////////////////

echo "<center><u> " . $creature . "'s Attack</u><br>";


 
 ///////hit location//////////////////////////////////////
		$randlocation = rand(0,100);
		if ($randlocation <= 15)
		{$location = "arms";}
		elseif ($randlocation <= 70)
		{$location = "chest";} 
		elseif ($randlocation <= 90)
		{$location = "legs";}
		elseif ($randlocation <= 100)
		{$location  = "head";}
		
   $playerarmor="SELECT * from playerarmor where pid='$pid' AND location='$location' AND equip=1";
   $playerarmor2=mysqli_query($db,$playerarmor) or die("Could not get player armmor");
   $playerarmor3=mysqli_fetch_array($playerarmor2);
   
   $noarmor = 0;
   
   if(is_null($playerarmor3['rating']))
   {
   $playerarmor3['rating'] = 0;
   $noarmor = 1;
   } 
  
  
   ////////////////////////////////////
$creatureattack = rand(1,20) + $creatureattack;
$playerdefense = rand(1,20) + $playerdefense + $playerarmor3['rating'];

echo $creature . "'s Attack roll is " . $creatureattack . "<br>";
echo $playerinfo3['name'] . "'s defense roll is " . $playerdefense . "<br>";

if ($creatureattack  > $playerdefense)
{

  echo $creature . " hits for a strike to the " . $location . "<br>";
   $creaturedamage = rand(1,6);
   $newplayerhp = $playerhp - $creaturedamage;
   echo "For " . $creaturedamage . " points of damage. <br>";
  if ($noarmor == 1)
  {
  echo "You are not wearing armor on your " . $location . ".<br>";
   $extradamage = rand(1,3);
   echo "You take an additional " . $extradamage . " points of damage.<br>";
  $newplayerhp = $newplayerhp - $extradamage;
  }
     if ($newplayerhp < 1)
   {
     echo $playerinfo3['name'] . " has been killed<br>";
      echo "<a href='gameover.php>Continue'";
      exit;
   }
  $updateplayer="update players set hpoints='$newplayerhp' where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");
}
else
{
  echo $creature . " misses!";
}
 echo "<br><br><a href='battle.php?creature=$creature'>Battle Again!";


//////////////////////first function
function creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature)
{
	$isgold = rand(0,$creaturegold);
	$isloot = rand(0,100);

	echo "You loot " . $isgold . " gold pieces.<br>";

	$updateplayer="update players set gold=gold+'$isgold' where name='$player'";
  mysqli_query($db,$updateplayer) or die("Could not update player");
  
	if($isloot < $hasloot)
	{
	echo "You find a " . $creatureloot . " on the " . $creature . ".<br>";
	/////////////////pull the items info
	 $critem="SELECT * from store where name='$creatureloot'";
   $critem2=mysqli_query($db,$critem) or die("Could not get creature item");
   $critem3=mysqli_fetch_array($critem2);
	/////////////////give it to player
	$randid4 = rand(999,99999999);
	$itembought = "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES 				('$pid','$critem3[name]','$critem3[stats]','$critem3[statadd]','$randid4','$critem3[type]')";
	mysqli_query($db,$itembought) or die("could not insert item into backpack");

	}
	else 
	{
	echo " You find no items of value on the creature.<br>";
}

}


 ?>
 </div>

User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Problem with attack.php....

Post by MikuzA »

your $db variable not set inside the function. In order to use a resource from outside the function, you must global declare it inside the function.



So add this inside the function,

Code: Select all

function creaturekilled($creaturegold,$creatureloot,$hasloot,$player,$pid,$creature)
{
 global $db;
...
}
Or pass the $db to the function as a parameter, whatever feels the most comfortable with you :)

Source:
http://php.net/manual/en/language.variables.scope.php
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
lunaticthrasher
Posts: 2
Joined: Thu Oct 23, 2014 4:26 am

Re: Problem with attack.php....

Post by lunaticthrasher »

Wow thanks man, thats what i needed! much appreciate the help and the link, still learning php and mysql a little at a time.
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Problem with attack.php....

Post by MikuzA »

No problem!
Keep up the good work!
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Post Reply

Return to “Older Browser MMO Videos”