Extracting data from mysql using php and using in html <a> tag

Extracting data from mysql using php and using in html <a> tag
0

#1

Hi,
As part of a course I’m enrolled on, I have to extract first names($first_name) and last names($last_name) from a mysql database using php. I then have to display them as hyperlinks (in a html table) that links to a page(view.php) that displays the users’ individual details using the profile id ($profile_id) as a GET response.

I can extract the data and link to view.php but I cannot get the variable values to display as hyperlink text. Instead it displays as code eg: “htmlentities((”$trow[“first_name”]".$trow[“last_name”]))".

Over the past 3 days I have tried various combinations of single and double quotes with concatenation, read php documentation and combed the web to no avail. Its driving me crazy and any help would be much appreciated.

Here is the line of code:

echo ('<a href="view.php?profile_id='.$trow['profile_id'].'">htmlentities(("$trow["first_name"]".$trow["last_name"]))</a>');


Freecodecamp PHP Project Group!
#2

You could try:

echo ('<a href="view.php?profile_id='.$trow['profile_id'].'">'.htmlentities($trow['first_name'].' '.$trow['last_name']).'</a>');

#3

Ah man, I was hoping you’d pick this up. Thank you so much. That works perfectly. I must say I’m struggling to properly understand string interpolation and concatenation when it comes to variables. I thought that to expand variables in a string we had to use double quotes?
After viewing the following code parsed by the browser:

echo ('<a href="view.php?profile_id='.$trow['profile_id'].'">test</a></br>');
echo ('<a href="view.php?profile_id='.$trow['profile_id'].'">'.htmlentities($trow['first_name']).' test1</a></br>');
echo ('<a href="view.php?profile_id=1">test2</a></br>');
echo ('<a href="view.php?profile_id='.$trow['profile_id'].'">'.'</a>1</br>2</br>3</br>4');

I can see that echo ('<a href="view.php?profile_id=">test</a>') is the actual html string with ?profile_id used to pass the $_GET array. With the strings '.$trow['profile_id'].' and '.htmlentities($trow['first_name'].' and '.$trow['last_name']).' inserted/concatenated into it.
I must say I am struggling to properly understand string interpolation and concatenation when it comes to variables. I thought that to expand variables in a string we had to use double quotes?

Thanks again.


#4

After thinking about it overnight, I thought of another way to write this in a cleaner way. You will need to use the extract function before the line, so the key names in $trow become variable names.

extract($trow);
echo "<a href='view.php?profile_id=$profile_id'>".htmlentities($first_name.' '.$last_name)."</a>";

I think this is much easier to read.