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!

                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?


Recommended Answers

All 2 Replies

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>';

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>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.