markvaughn2006 Posted October 4, 2009 Share Posted October 4, 2009 How do you echo a link in a variable form while? Thanks guys!! <?php $result = mysql_query("SELECT * FROM locations WHERE owned='$_SESSION[myusername]'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo "<a href="$row['link']">$row['title']</a>"; echo "<br/>"; } ?> ERROR is... Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/properties.php on line 175 Quote Link to comment Share on other sites More sharing options...
jon23d Posted October 4, 2009 Share Posted October 4, 2009 echo "<a href="$row['link']">$row['title']</a>"; should be echo "<a href="{$row['link']}">{$row['title']}</a>"; Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 4, 2009 Share Posted October 4, 2009 jon23d almost had it; echo "<a href=\"{$row['link']}\">{$row['title']}</a>"; You should also wrap myusername in single quotes (it's not a constant, am I right?), and then wrap the whole session variable in curly brackets. mysql_query("SELECT * FROM locations WHERE owned = '{$_SESSION['myusername']}'") Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 you could also do this echo "<a href='".$row['link']."'>".$row['title']."</a>"; Quote Link to comment Share on other sites More sharing options...
markvaughn2006 Posted October 4, 2009 Author Share Posted October 4, 2009 cool it worked! thanks a lot!! Whats the reason for.. "You should also wrap myusername in single quotes (it's not a constant, am I right?), and then wrap the whole session variable in curly brackets."?? and no it is not a constant Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 because you are supposed to surround associative array strings with single quotes. a string that isn't surrounded by quotes is assumed to be a constant. For example DEFINE("MY_CONST", "SOME VALUE"); //if i want to use this constant echo MY_CONST; PHP is smart enough to realize that if a constant doesn't exist, you probably meant a string, but if you have a case where you want to say the string MY_CONST, instead of his value, when you do this echo MY_CONST when you meant echo "MY_CONST"; you will run into problems in your case, saying $myARR[mykey]; is technically OK as long as you dont have a constant named mykey, but its best practice to avoid instances when this could be a problem by surrounding your array keys with single quotes. oh and you surround it with curly brackets, because thats how you output associative arrays without concatenating strings Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 4, 2009 Share Posted October 4, 2009 mikesta707 beat me to it, but here's my (pretty identical) explanation. Whats the reason for.. "You should also wrap myusername in single quotes (it's not a constant, am I right?), and then wrap the whole session variable in curly brackets."?? and no it is not a constant myusername in your case is a key in an associative array (meaning the keys are string, not integers), and therefore it should be in quotes. Just like you would write $key = 'myusername'; echo $_SESSION[$key]; and not $key = myusername; echo $_SESSION[$key]; Without quotes PHP thinks it's a constant, and looks for one with that particular name. If it doesn't exist, PHP assumes it's a string (while issuing a notice), and that's why your syntax works. Note: When testing it, it seems no notices are issued when the "$array[key]" syntax is used though. The curly brackets are just the correct syntax when using array values of associative arrays inside double quoted strings. Quote Link to comment Share on other sites More sharing options...
markvaughn2006 Posted October 4, 2009 Author Share Posted October 4, 2009 cool thanks! Quote Link to comment 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.