raider00321 Posted February 10, 2008 Share Posted February 10, 2008 Hey guys, i have a slight problem in writting this code... if($_POST[yourbuildings]) { print"<center><font size='6'>Your Buildings</font></center> <table width='720'><tr>"; $query = "SELECT * FROM table2"; $result = mysql_query($query)or die(mysql_error()); while($row = mysql_fetch_array($result)) { $building=mysql_fetch_array(mysql_query("SELECT * FROM table1 WHERE name='$userstats3[userid]'"))or die(mysql_error()); $amt=$row[name]; print"<th width='200'>$row[name]</th><th width='200'>$building[$amt]</th></tr><tr>"; } } This code works fine on my test server at home, but when its on the server, it doesnt work at all.... So now im trying to find other options on writting it. Does anyone know another way or know how to fix this problem? Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/ Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 where is $userstats3[userid] defined? try putting error_reporting(E_ALL); at top of your code Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463176 Share on other sites More sharing options...
raider00321 Posted February 10, 2008 Author Share Posted February 10, 2008 userstats3[userid is defined at the very begginning of th script and i tried error reporting, but nothing came up Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463184 Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 No errors! I would have expected a screenful from that code Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463189 Share on other sites More sharing options...
raider00321 Posted February 10, 2008 Author Share Posted February 10, 2008 lol, why? like i said, it worked perfectly on the test server, its just that nothing actually appeared in the result on the proper server Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463192 Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 lol, why? Whenever you have an associative index, such as $x[a] then the compiler will assume that the "a" is constant (since it isn't in quotes to say it's a string literal and there is no $ to say it's a variable.) It now searches for constant definitions and when it doesn't find one it assumes you must mean $x['a'] and then puts out a notice (if the reporting level is E_ALL) Notice: Use of undefined constant a - assumed 'a' in xxx.php on line n You should have had all instances reported. This is slowing down your script every time you do it. Always quote the string key values. If referencing such array elements in a string it is always safer to enclose it in {} eg ... WHERE name='{$userstats3['userid']}'") That query inside the loop is always going to pull the same record, so why not put it outside the loop and just call it once? Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463203 Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 I forgot, put sql string in a variable so you you can echo and check it. $sql = "SELECT * FROM table1 WHERE name='$userstats3[userid]'"; echo $sql; // check it looks OK $building=mysql_fetch_array(mysql_query($sql))or die(mysql_error()); It's a bad idea to chain mysql functions like that. It stops you error checking and in other cases the logic can be plain wrong. Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463204 Share on other sites More sharing options...
raider00321 Posted February 10, 2008 Author Share Posted February 10, 2008 hmmmm ok, the reason i had it diplayed like that is because its a mysql array. This makes no difference? Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463207 Share on other sites More sharing options...
trq Posted February 10, 2008 Share Posted February 10, 2008 This makes no difference? No, always quote string key values. Quote Link to comment https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/#findComment-463236 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.