gamesmstr Posted October 3, 2008 Share Posted October 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/ Share on other sites More sharing options...
ratcateme Posted October 3, 2008 Share Posted October 3, 2008 try adding this between the two mysql functions echo mysql_num_rows($dnpcgrid2)."\r\n"; Scott. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656223 Share on other sites More sharing options...
trq Posted October 3, 2008 Share Posted October 3, 2008 I have checked the tables that the records failing DO exist but do NOT after the code is executed. Well what is removing them? This code certainly isn't. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656224 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656228 Share on other sites More sharing options...
ratcateme Posted October 3, 2008 Share Posted October 3, 2008 change your query to $dnpcgrid2 = mysql_query($test) or die(mysql_error()); Scott. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656229 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 try adding this between the two mysql functions echo mysql_num_rows($dnpcgrid2)."\r\n"; Scott. I tried that and when it fails I get 0 Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656230 Share on other sites More sharing options...
ratcateme Posted October 3, 2008 Share Posted October 3, 2008 that is because there is no row in the database with the ID that you are trying to find mysql_fetch_array() will fail if your query returns zero rows Scott. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656231 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 change your query to $dnpcgrid2 = mysql_query($test) or die(mysql_error()); Scott. No error is returned. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656234 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 No. I am looking at the record in the database. I know it is there. And if I remove the delete query, it finds it EVERY time. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656235 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 Anybody else? This is driving me crazy! Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656269 Share on other sites More sharing options...
revraz Posted October 3, 2008 Share Posted October 3, 2008 You need to do some basic troubleshooting. Echo your query so you can see if the variable is passing correctly. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656302 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656312 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 I've tried everything I can think of and nothing seems to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656370 Share on other sites More sharing options...
aschk Posted October 3, 2008 Share Posted October 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656387 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 Yeah, I stripped out the top.php call and it works. Great. That file is a nightmare. At least I know where to look now. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656476 Share on other sites More sharing options...
gamesmstr Posted October 3, 2008 Author Share Posted October 3, 2008 This appears to be a firefox issue. It is clicking the entry twice which is causing the bug. Anybody know a workaround for it? Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656556 Share on other sites More sharing options...
gamesmstr Posted October 4, 2008 Author Share Posted October 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126877-solved-similar-queries-but-1-doesnt-work/#findComment-656930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.