Jump to content

Another way to write this....


raider00321

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/90337-another-way-to-write-this/
Share on other sites

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?

 

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.