1

Does anyone know how to make a link out of a php variable. I want my php variable $LeagueLink to be a link to leaguehome with the name of the result of a mysql query. Please help if you can....

$result = mysql_query("SELECT League FROM League_Info WHERE User_ID = '$id'");
$result2 = mysql_fetch_array($result);
    $result3 = $result2['League'];
    $LeagueLink = '<a href="home.com/test.php"><?=$result3?></a>';
1
  • security tip: use prepared statements Commented Jul 22, 2012 at 2:33

2 Answers 2

4
$LeagueLink = "<a href=\"http://home.com/leaguehome.php\">$result3</a>";

To place variables directly into a string like above, it needs to be a double quote string ("), not a single quote string (')

Or, if you're worried about carelessness-provoked errors, use string concatenation:

$LeagueLink = '<a href="http://home.com/leaguehome.php">' . $result3 . '</a>';
Sign up to request clarification or add additional context in comments.

4 Comments

Please don't embed variables in strings, it is subject to errors if done carelessly.
"<a href=\"home.com/leaguehome.php\">$result3</a>" should be "<a href=\"home.com/leaguehome.php\">".$result3."</a>"
And when you're at it, if you don't need metacharacters in the string, change the double-quotes to single-quotes. And don't forget to put http:// before home.com, so the link works correctly.
@sgielen: I need the double quotes for embedding variables. I used single quotes on the concatenation alternative
1

You can also do this

$LeagueLink = '<a href="home.com/leaguehome.php">'.$result3.'</a>';

2 Comments

what about with this... $LeagueLink = "<a href="leaguehome.php">$result3</a>";
You need to escape the double quotes. You can't put double quotes with double quotes. It is either ' "a" ' or " 'a' "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.