Issue with using php variable in hyperlink

2019-09-20 01:01发布

问题:

How do you use one php variable for the url in a hyperlink around another php variable? Below is my code, which is clearly not working. I want $url to be the hyperlinked text of $songTitle.

if ($result) {
    $numberOfRows = $result->num_rows;

    for($i=0; $i < $numberOfRows; $i++) {
        $row = $result->fetch_assoc();
        echo '<tr>';
        echo '<td>' . $row['artistName'] . '</td>';
        echo '<td>'<a href=". $row['url'] . > . $row['songTitle'] . </a> '</td>';
        echo '<td>' . $row['yOR'] . '</td>';
        echo '</tr>';
    }
}

回答1:

You are not concatenating your strings properly.

for($i=0; $i < $numberOfRows; $i++) {
        $row = $result->fetch_assoc();
        echo '<tr>';
        echo '<td>' . $row['artistName'] . '</td>';
        echo '<td><a href="' . $row['url'] . '" >' . $row['songTitle'] . '</a></td>';
        echo '<td>' . $row['yOR'] . '</td>';
        echo '</tr>';
    }


回答2:

did you copy your script from somewhere? In parts of it you seem to understand how to escape and use PHP values inside a string, but then you messed up the one line.

Anyway, this is it fixed.. you didn't have your quotes or single quotes used properly.

echo "<td><a href='". $row['url'] . "'>" . $row['songTitle'] ." </a> </td>";