1,105,352 Community Members

Making a field in mysql in a link

Member Avatar
GlenRogers
Posting Whiz
329 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This is probably simple enough to do, but I cant get it!
I have a mysql table 'links' with these fields;

id int 10 primary ai
link varchar 200
description varchar 200

I have a links page to populate with this info. I want the desciption as the visible text.

This is my code which doesnt work!

 <?php 
                include 'connect.php';
                $data = mysql_query("SELECT * FROM links ORDER BY id DESC")
                or die(mysql_error());

                while($info = mysql_fetch_array( $data )) { 
                echo '<a href="$info['link']">$info['description']</a>';//this line is the problem
                echo '<br />';
                }
              ?> 

Can anyone see where im going wrong?

Thanks....

Member Avatar
AleMonteiro
Master Poster
758 posts since Aug 2010
Reputation Points: 115 [?]
Q&As Helped to Solve: 143 [?]
Skill Endorsements: 29 [?]
 
0
 

I think it's just a syntax problem... It's been a while since I coded in PHP, but this should work:

echo '<a href="'.$info['link'].'">.'$info['description'].'</a>';
Member Avatar
EvolutionFallen
Posting Pro
504 posts since Aug 2009
Reputation Points: 107 [?]
Q&As Helped to Solve: 108 [?]
Skill Endorsements: 8 [?]
 
0
 

AleMonteiro is correct. The way you originally put the variables in does not replace them with their values in the echo statement. If you want to stay with single quotes, you need to use the concatenation operator (.) as shown above. If you want to put the variable in without the concatenation operator, have to use double quotes around the entire string, escape the double quotes for the href, and since you're referring to array indexes you must add curly braces around the variables too:

echo "<a href=\"{$info['link']}\">{$info['description']}</a>';
Question Answered as of 1 Year Ago by EvolutionFallen and AleMonteiro
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article