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
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?

 

Link to comment
Share on other sites

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.

 

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.