Page 1 of 1
mysqli problems...
Posted: Sat Sep 11, 2010 12:23 pm
by alexander19
I got a weird thing going on:
Code: Select all
if(isset($_POST['pid1']) && isset($_POST['pid2'])) {
$pid1 = $_POST['pid1'];
$pid2 = $_POST['pid2'];
mysqli_query($db,"INSERT into arenainfo (pid1,pid2,) VALUES ('$pid1','$pid2')");
}
The weird thing is that the 2 pid values wont be inserted into the database,I tried to echo the values and see if they existed and it echoed them.
I cant see anything wrong with that code...
And my second issue is on sending multiple queries by using mysqli_query.
From what I heard mysqli_query sends only 1 query at a time ,not like mysql_query which can send an unlimited amount.
So I googled on this a little and found out that some people used mysqli_multi_query..but this didnt work either.
My code is something like this:
Code: Select all
for($i=1;$i<=3;$i++)
{
echo "player1 attacks player2 for 3 points of damage";
mysqli_query($db,"update players set hpoints=hpoints-3 where name='$player2'");
}
The whole code sends only 1 query to the players database..same if I use mysqli_multi_query..when it should have descreased the player's hpoints by 9..it only descreases by 3.
Someone got any ideeas on this?
Re: mysqli problems...
Posted: Sat Sep 11, 2010 4:33 pm
by Zyviel
alexander19 wrote:I got a weird thing going on:
Code: Select all
if(isset($_POST['pid1']) && isset($_POST['pid2'])) {
$pid1 = $_POST['pid1'];
$pid2 = $_POST['pid2'];
mysqli_query($db,"INSERT into arenainfo (pid1,pid2,) VALUES ('$pid1','$pid2')");
}
You have a comma after pid2. I think that will cause some problems.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 4:38 pm
by Zyviel
And my second issue is on sending multiple queries by using mysqli_query.
From what I heard mysqli_query sends only 1 query at a time ,not like mysql_query which can send an unlimited amount.
So I googled on this a little and found out that some people used mysqli_multi_query..but this didnt work either.
My code is something like this:
Code: Select all
for($i=1;$i<=3;$i++)
{
echo "player1 attacks player2 for 3 points of damage";
mysqli_query($db,"update players set hpoints=hpoints-3 where name='$player2'");
}
The whole code sends only 1 query to the players database..same if I use mysqli_multi_query..when it should have descreased the player's hpoints by 9..it only descreases by 3.
Someone got any ideeas on this?
I don't know if you can put an equation in your sql code like that.
You might be able to do this:
$hpoints = $hpoints-3;
mysqli_query($db,"update players set hpoints=$hpoints where name='$player2'");
Re: mysqli problems...
Posted: Sat Sep 11, 2010 5:05 pm
by alexander19
Thanks man...damn didnt see that coma.
As for the second problem,you can have that type of equations but even if I remove it and declare a variable before it like $newhp = $hpoints -3;
and then "UPDATE players set hpoints = '$newhp'" it still wont work.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 6:24 pm
by Jackolantern
alexander19 wrote:Thanks man...damn didnt see that coma.
As for the second problem,you can have that type of equations but even if I remove it and declare a variable before it like $newhp = $hpoints -3;
and then "UPDATE players set hpoints = '$newhp'" it still wont work.
That is really strange because I have been looking back and forth between your query and one in one of my scripts, and they are basically identical (outside from the equation in the query, which I usually don't do). I have been staring at it for about 10 minutes and I don't see anything different than in my script.
Can you try writing it out figuring the equation outside of the query, try running it, and then post that code if it doesn't work? Maybe it will help us diagnose the problem. Also, try adding and printing a check to your query, like:
Code: Select all
$queryResult = mysqli_query(....);
echo "Result of the query on iteration ".$i." resulted in: ".$queryResult;
That way you can have a bit more diagnostic text.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 6:31 pm
by Zyviel
When the sql statement fails do you see an error message on the webpage? I can't seem to find a syntax error with the sql statement. You may want to double check the spelling of the fields and table name with whats in your sql database. You can also try to run the query in the admin interface for your sql database and see if it runs ok there.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 6:33 pm
by Jackolantern
He said it actually is running the query and reducing the health value by 3, it is just not taking it down 9 like it is supposed to. I have been looking over the FOR structure several times, too.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 7:26 pm
by alexander19
I tried the printing check and it returns:
Result of the query on iteration 1 resulted in: 1
Result of the query on iteration 2 resulted in: 1
Result of the query on iteration 3 resulted in: 1
And this is the code :
Code: Select all
$select = mysqli_fetch_assoc(mysqli_query($db,"SELECT * FROM arenainfo WHERE pid1='$pid' OR pid2='$pid'"));
////////////// ARENAINFO END ////////////
$pid1 = $select['pid1'];
$pid2 = $select['pid2'];
//gets players info
$playerinfo1 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from players where id='$pid1'"));
$playerinfo2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from players where id='$pid2'"));
$name1 = $playerinfo1['name'];
$name2 = $playerinfo2['name'];
$pl1 = $playerinfo1['level'];
$pl2 = $playerinfo2['level'];
for($i=1;$i<=3;$i++)
{
$p1weaponinfo3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerweapons where equip=1 AND pid='$pid1'"));
$p2weaponinfo3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerweapons where equip=1 AND pid='$pid2'"));
$p1att2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name1'"));
$p1att3 = $p1att2['pid'];
$p1att5 = mysqli_fetch_assoc(mysqli_query($db,"SELECT * from researched where pid = '$p1att3'"));
$p1att6 = $p1att5['attack'] * 10;
$player1attack = rand(1,$pl1) + $playerinfo1['attack'] + $p1weaponinfo3['rating'] + $p1att6;
//////////////////////////////////////////////////////
$randlocation = rand(0,100);
if ($randlocation <= 15)
{$locationn = "hands";}
elseif ($randlocation <= 70)
{$locationn = "chest";}
elseif ($randlocation <= 90)
{$locationn = "feet";}
elseif ($randlocation <= 100)
{$locationn = "head";}
$player2armor3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerarmor where pid='$pid2' AND location='$locationn' AND equip=1"));
$p2def2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name2'"));
$p2def3 = $p2def2['pid'];
$p2def5 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from researched where pid = '$p2def3'"));
$p2def6 = $p2def5['defense'] * 10;
$player2defense = rand(1,$pl2) + $playerinfo2['defense'] + $player2armor3['rating'] + $p2def6;
if ($player1attack > $player2defense)
{
if(!$p1weaponinfo3['hittext'])
{
echo "<b>" .$name1 . " jumps towards his enemy and hits!</b><br>";
}
else
{
echo "<b>" .$name1 . " " . $p1weaponinfo3['hittext'] . " and hits!</b><br>";
}
$weapdamage = $p1weaponinfo3['damage'];
$halfdamage = $p1weaponinfo3['damage'] / 2;
$playerdamage = rand($halfdamage,$weapdamage) + 1;
$newplayer2hp = $playerinfo2['hpoints'] - $playerdamage;
$queryResult=mysqli_query($db,"update players set hpoints='$newplayer2hp' where name='$name2'");
echo "Result of the query on iteration ".$i." resulted in: ".$queryResult;
echo "<b>For <font color='#39533a'; style='font-weight:bold'>" . $playerdamage . "</font> points of damage.</b> <br><br>";
}
else
{
if(!$p1weaponinfo3['hittext'])
{
echo "<b>" .$name1 . " misses!</b><br><br>";
}
else
{
echo "<b>" .$name1 . " " . $p1weaponinfo3['hittext'] . " and misses!</b><br><br>";
}
}
///////////////////////////////// player2///////////////////////////////////////////////////
$p2weaponinfo3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerweapons where equip=1 AND pid='$pid2'"));
$p2att2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name2'"));
$p2att3 = $p2att2['pid'];
$p2att5 = mysqli_fetch_assoc(mysqli_query($db,"SELECT * from researched where pid = '$p2att3'"));
$p2att6 = $p2att5['attack'] * 10;
$player2attack = rand(1,$pl2) + $playerinfo2['attack'] + $p2weaponinfo3['rating'] + $p2att6;
//////////////////////////////////////////////////////
$randlocation = rand(0,100);
if ($randlocation <= 15)
{$locationn = "hands";}
elseif ($randlocation <= 70)
{$locationn = "chest";}
elseif ($randlocation <= 90)
{$locationn = "feet";}
elseif ($randlocation <= 100)
{$locationn = "head";}
$player1armor3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerarmor where pid='$pid2' AND location='$locationn' AND equip=1"));
$p1def2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name1'"));
$p1def3 = $p1def2['pid'];
$p1def5 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from researched where pid = '$p1def3'"));
$p1def6 = $p1def5['defense'] * 10;
$player1defense = rand(1,$pl2) + $playerinfo2['defense'] + $player1armor3['rating'] + $p1def6;
if ($player2attack > $player1defense)
{
if(!$p2weaponinfo3['hittext'])
{
echo "<b>" .$name2 . " jumps towards his enemy and hits!</b><br>";
}
else
{
echo "<b>" .$name2 . " " . $p2weaponinfo3['hittext'] . " and hits!</b><br>";
}
$weapdamage1 = $p2weaponinfo3['damage'];
$halfdamage1 = $p2weaponinfo3['damage'] / 2;
$player1damage = rand($halfdamage1,$weapdamage1) + 1;
$newplayer1hp = $playerinfo1['hpoints'] - $player1damage;
$queryResult=mysqli_query($db,"update players set hpoints='$newplayer1hp' where name='$name2'");
echo "Result of the query on iteration ".$i." resulted in: ".$queryResult;
echo "<b>For <font color='#39533a'; style='font-weight:bold'>" . $player1damage . "</font> points of damage.</b> <br><br>";
}
else
{
if(!$p2weaponinfo3['hittext'])
{
echo "<b>" .$name2 . " misses!</b><br><br>";
}
else
{
echo "<b>" .$name2 . " " . $p2weaponinfo3['hittext'] . " and misses!</b><br><br>";
}
}
}
?>
Tried running this at the beggining of the page and it worked,so I guess the problem is from the script itself
Code: Select all
for($i=1;$i<=2;$i++){
mysqli_query($db,"UPDATE players set hpoints = hpoints-5 where name = '$player'");}
Re: mysqli problems...
Posted: Sat Sep 11, 2010 9:02 pm
by Jackolantern
Two notes here:
1. You need a lot more commenting in your code. Even if you are the only one working with it, I can guarantee you that you will have a very hard time figuring out what you were doing when you come back to this script a few months down the line. Heck, I have not been able to figure out what I was thinking just 3 or 4 days later. Even if it seems obvious at the time, every block and every action needs thorough explanation. And don't tell yourself you will go back and do it later, because no one ever does. It must be written at the time the code is written. It is also a huge help for other on the forum to help diagnose problems.
2. What part is going wrong now? I assumed the earlier issue with the looping of that update query was written correctly. But now what is going wrong in the full script? Without commenting and without knowing what is going wrong, it would be extremely hard to spot problems.
Re: mysqli problems...
Posted: Sat Sep 11, 2010 9:47 pm
by alexander19
Sorry for that...here is the commented script.The problem is still the for loop,it updates the player's hpoints only 1 time.
Code: Select all
<?php
////////////// getting the stats from arenainfo database table ////////////
$select = mysqli_fetch_assoc(mysqli_query($db,"SELECT * FROM arenainfo WHERE pid1='$pid' OR pid2='$pid'"));
$pid1 = $select['pid1'];
$pid2 = $select['pid2'];
//getting the players info
$playerinfo1 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from players where id='$pid1'"));
$playerinfo2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from players where id='$pid2'"));
$name1 = $playerinfo1['name'];
$name2 = $playerinfo2['name'];
$pl1 = $playerinfo1['level'];
$pl2 = $playerinfo2['level'];
///starting the 3 rounds battle loop
for($i=1;$i<=3;$i++)
{
////getting player1's attack stats
$p1weaponinfo3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerweapons where equip=1 AND pid='$pid1'"));
$p1att2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name1'"));
$p1att3 = $p1att2['pid'];
$p1att5 = mysqli_fetch_assoc(mysqli_query($db,"SELECT * from researched where pid = '$p1att3'"));
$p1att6 = $p1att5['attack'] * 10;
/////////calculating the attack chance of player1
$player1attack = rand(1,$pl1) + $playerinfo1['attack'] + $p1weaponinfo3['rating'] + $p1att6;
$randlocation = rand(0,100);
if ($randlocation <= 15)
{$locationn = "hands";}
elseif ($randlocation <= 70)
{$locationn = "chest";}
elseif ($randlocation <= 90)
{$locationn = "feet";}
elseif ($randlocation <= 100)
{$locationn = "head";}
////getting player2's defense stats
$player2armor3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerarmor where pid='$pid2' AND location='$locationn' AND equip=1"));
$p2def2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name2'"));
$p2def3 = $p2def2['pid'];
$p2def5 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from researched where pid = '$p2def3'"));
$p2def6 = $p2def5['defense'] * 10;
/////////calculating the defense chance of player2
$player2defense = rand(1,$pl2) + $playerinfo2['defense'] + $player2armor3['rating'] + $p2def6;
if ($player1attack > $player2defense)
{
if(!$p1weaponinfo3['hittext'])
{
echo "<b>" .$name1 . " jumps towards his enemy and hits!</b><br>";
}
else
{
echo "<b>" .$name1 . " " . $p1weaponinfo3['hittext'] . " and hits!</b><br>";
}
////calculating player1's damage and updating player2's hpoints
$weapdamage = $p1weaponinfo3['damage'];
$halfdamage = $p1weaponinfo3['damage'] / 2;
$playerdamage = rand($halfdamage,$weapdamage) + 1;
$newplayer2hp = $playerinfo2['hpoints'] - $playerdamage;
mysqli_query($db,"update players set hpoints='$newplayer2hp' where name='$name2'");
echo "<b>For <font color='#39533a'; style='font-weight:bold'>" . $playerdamage . "</font> points of damage.</b> <br><br>";
}
else
{
if(!$p1weaponinfo3['hittext'])
{
echo "<b>" .$name1 . " misses!</b><br><br>";
}
else
{
echo "<b>" .$name1 . " " . $p1weaponinfo3['hittext'] . " and misses!</b><br><br>";
}
}
////////retrieving player2's attack stats
$p2weaponinfo3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerweapons where equip=1 AND pid='$pid2'"));
$p2att2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name2'"));
$p2att3 = $p2att2['pid'];
$p2att5 = mysqli_fetch_assoc(mysqli_query($db,"SELECT * from researched where pid = '$p2att3'"));
$p2att6 = $p2att5['attack'] * 10;
/////calculating player2's attack chance
$player2attack = rand(1,$pl2) + $playerinfo2['attack'] + $p2weaponinfo3['rating'] + $p2att6;
//////////////////////////////////////////////////////
$randlocation = rand(0,100);
if ($randlocation <= 15)
{$locationn = "hands";}
elseif ($randlocation <= 70)
{$locationn = "chest";}
elseif ($randlocation <= 90)
{$locationn = "feet";}
elseif ($randlocation <= 100)
{$locationn = "head";}
///retrieving player1's defense stats
$player1armor3 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from playerarmor where pid='$pid2' AND location='$locationn' AND equip=1"));
$p1def2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from guildmembers where name='$name1'"));
$p1def3 = $p1def2['pid'];
$p1def5 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from researched where pid = '$p1def3'"));
$p1def6 = $p1def5['defense'] * 10;
//////calculating player1's defense chance
$player1defense = rand(1,$pl2) + $playerinfo1['defense'] + $player1armor3['rating'] + $p1def6;
if ($player2attack > $player1defense)
{
if(!$p2weaponinfo3['hittext'])
{
echo "<b>" .$name2 . " jumps towards his enemy and hits!</b><br>";
}
else
{
echo "<b>" .$name2 . " " . $p2weaponinfo3['hittext'] . " and hits!</b><br>";
}
//////player2's damage and updating player1 hpoints
$weapdamage1 = $p2weaponinfo3['damage'];
$halfdamage1 = $p2weaponinfo3['damage'] / 2;
$player1damage = rand($halfdamage1,$weapdamage1) + 1;
$newplayer1hp = $playerinfo1['hpoints'] - $player1damage;
mysqli_query($db,"update players set hpoints='$newplayer1hp' where name='$name2'");
echo "<b>For <font color='#39533a'; style='font-weight:bold'>" . $player1damage . "</font> points of damage.</b> <br><br>";
}
else
{
if(!$p2weaponinfo3['hittext'])
{
echo "<b>" .$name2 . " misses!</b><br><br>";
}
else
{
echo "<b>" .$name2 . " " . $p2weaponinfo3['hittext'] . " and misses!</b><br><br>";
}
}
}
?>