Jump to content

[SOLVED] Similar queries but 1 doesn't work.


gamesmstr

Recommended Posts

Ok, odd situation here.  I have a query select that seems to work about one in 20 times.  To set it up, npcid is passed through a link and retrieved by $_GET.  I have been testing conditions to see what fails so there is a lot of stuff in here I don't need.  Let me also preface this by saying I have checked the tables that the records failing DO exist but do NOT after the code is executed.

 

This is what fails

 

$test="SELECT * FROM tbl_dungeon_npcgrid WHERE id='$npcid'";

echo "$test<br>";

$dnpcgrid2 = mysql_query($test);

$dnpcgrid = mysql_fetch_array($dnpcgrid2);

if (!$dnpcgrid2){ echo "fail 1 ";} else{ echo"pass 1 "; print_r($dnpcgrid2);}

if (!$dnpcgrid){ echo "fail 2 ";} else{ echo"pass 2 "; print_r($dnpcgrid);}

echo"test = $npcid<br>";

echo"test = $dnpcgrid[id]<br>";

 

 

This is the result when it fails:

 

SELECT * FROM tbl_dungeon_npcgrid WHERE id='12414680'

pass 1 Resource id #13fail 2 test = 12414680

test =

 

This is the result when it passes:

 

SELECT * FROM tbl_dungeon_npcgrid WHERE id='12414699'

pass 1 Resource id #13pass 2 Array ( [0] => 12414699 [id] => 12414699 [1] => 34 [xco] => 34 [2] => 50 [yco] => 50 [3] => 1 [zco] => 1 [4] => 41 [npcid] => 41 [5] => 321 [level] => 321 ) test = 12414699

test = 12414699

 

 

 

Link to comment
Share on other sites

ok.  There is a delete query MUCH further down.  I just checked and when I comment it out.  The above code works fine.

 

Here is the code for the delete.

 

$deletenpc = mysql_query("DELETE FROM tbl_dungeon_npcgrid WHERE id='$npcid'");
echo "<br>You have killed the <font color=black>$dnpc[name]</font>!<br>You gained <font color=darkgreen>$exp</font> experience and <font color=black>$gold</font> gold!<br>"; 

 

When it fails, the echo never happens.  This snippet is not part of another function either.

Link to comment
Share on other sites

I have and it is.  Now for a twist.  I stripped out all the code to make sure I narrowed the problem down. The $_GET is in the included gameconfig.php.

 

<?php
session_start();
include("gameconfig.php");
$title = "Local Training Dungeon- Battle Monster";
if(!$email || !$password){
error("field");
exit;}
if($playerinfo[jailtime] > 0){
    error("jail");
exit;}
if($playerinfo[password] != $password){
error("password");}
else{
include("top.php");
//Get player location
$duser2 = mysql_query("SELECT * FROM tbl_dungeon_user WHERE id='$playerinfo[id]'");
$duser = mysql_fetch_array($duser2);
//get monster record
$dnpcgrid2 = mysql_query("SELECT * FROM tbl_dungeon_npcgrid WHERE id='$npcid'") or die(mysql_error());
$dnpcgrid = mysql_fetch_array($dnpcgrid2);
$dnpc2 = mysql_query("SELECT * FROM tbl_dungeon_npc WHERE id='$dnpcgrid[npcid]'");
$dnpc = mysql_fetch_array($dnpc2);

echo"test1<br>";
print_r($dnpcgrid);
print_r($dnpc);
echo"test2<br>";

//PERFORM CHECKS ON BOTH USER AND NPC
if(!$dnpcgrid){
	echo "<center>Either you were too slow and this NPC has now been defeated by someone else, or this NPC doesn't exist!";
	echo "<br><br><center>[<a href=game-dungeon.php?action=roam>Back to Grid Square</a>]</center>";
	include("bottom.php");
	exit;
}

//	mysql_query("DELETE FROM tbl_dungeon_npcgrid WHERE id='$npcid'");

include("bottom.php");
}?>

 

This code results in the following.

 

test1

Array ( [0] => 12414754 [id] => 12414754 [1] => 34 [xco] => 34 [2] => 50 [yco] => 50 [3] => 1 [zco] => 1 [4] => 39 [npcid] => 39 [5] => 276 [level] => 276 ) Array ( [0] => 39 [id] => 39 [1] => Stone Giant [name] => Stone Giant [2] => Stone Fist [wepname] => Stone Fist [3] => [image] => [4] => 270 [level] => 270 [5] => 310 [level2] => 310 [6] => 30000 [hp] => 30000 [7] => 2200 [agility] => 2200 [8] => 2195 [strength] => 2195 [9] => 1800 [armor] => 1800 [10] => 9500 [damage] => 9500 [11] => 90 [gold] => 90 [12] => 90 [exp] => 90 ) test2

 

 

Now, if I uncomment the delete query: and do it on the EXACT SAME record.

 

<?php
session_start();
include("gameconfig.php");
$title = "Local Training Dungeon- Battle Monster";
if(!$email || !$password){
error("field");
exit;}
if($playerinfo[jailtime] > 0){
    error("jail");
exit;}
if($playerinfo[password] != $password){
error("password");}
else{
include("top.php");
//Get player location
$duser2 = mysql_query("SELECT * FROM tbl_dungeon_user WHERE id='$playerinfo[id]'");
$duser = mysql_fetch_array($duser2);
//get monster record
$dnpcgrid2 = mysql_query("SELECT * FROM tbl_dungeon_npcgrid WHERE id='$npcid'") or die(mysql_error());
$dnpcgrid = mysql_fetch_array($dnpcgrid2);
$dnpc2 = mysql_query("SELECT * FROM tbl_dungeon_npc WHERE id='$dnpcgrid[npcid]'");
$dnpc = mysql_fetch_array($dnpc2);

echo"test1<br>";
print_r($dnpcgrid);
print_r($dnpc);
echo"test2<br>";

//PERFORM CHECKS ON BOTH USER AND NPC
if(!$dnpcgrid){
	echo "<center>Either you were too slow and this NPC has now been defeated by someone else, or this NPC doesn't exist!";
	echo "<br><br><center>[<a href=game-dungeon.php?action=roam>Back to Grid Square</a>]</center>";
	include("bottom.php");
	exit;
}

mysql_query("DELETE FROM tbl_dungeon_npcgrid WHERE id='$npcid'");

include("bottom.php");
}?>

 

 

I get the following results:

 

test1

test2

Either you were too slow and this NPC has now been defeated by someone else, or this NPC doesn't exist!

 

[back to Grid Square]

 

 

Now Can someone tell me why?

Link to comment
Share on other sites

There's clearly an error in your logic somewhere. We don't know what top.php contains nor what gameconfig.php does. There is no notion of the process of events so we don't know which page is being called subsequently, or which page the form information is coming from... therefore we have no idea or assurance of what variables are coming through.

 

You might also wish to consider renaming some of your variables as they are very ambiguous. They don't seem to mimic your query/result/array variables as they should  (i.e. $user_query = ....)

 

I'm also unsure how this test is going to work for you "...if(!$dnpcgrid){..."

By the looks of it this will either be an associative array, or null (if the mysql_fetch_array() fails).

And because you're using it in a boolean statement it needs to convert an array (or null) to a boolean value. Now i know that null is converted to false, however do you know what boolean value an array is converted to?

 

Also, if it's null (i.e. false), then !false will be true, implying that if you get a valid result, you're going to output the error message.

 

Anyway, without rewriting this whole thing i suggest you take a look at what you're doing and follow the logic steps needed.

Link to comment
Share on other sites

OK  An update for those who are interested.  I solved the problem by removing the meta tags from the html portion of my page.  Apparently Firefox sends links twice when they are in there.  This is what I deleted.

 

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<meta name="keywords" content="">

<meta name="description" content="">

 

 

So the problem is solved and I that everyone for their help.  It got me pointed in the right direction.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.