Search code examples
phpechohref

How to echo a href when using a variable from mysqli


I have a variable retrieved from a mysqli database called eBayURL. I am attempting to assign the variable to a button.

However I get an error when using

echo "<a target='"_blank"' href='".$row['eBayURL']."' class="btn btn-outlined btn-primary btn-md">Click Here <br> To Book Now </a></li>'";

I have also tried with { but also seems to fail. What is the correct syntax please?


Solution

  • You have an incorrect mix of single and double quotes.

    Try the following instead:

    echo '<a target="_blank" href="'.$row['eBayURL'].'" class="btn btn-outlined btn-primary btn-md">Click Here <br> To Book Now </a></li>';
    

    Or you can use:

    echo "<a target=\"_blank\" href=\"".$row['eBayURL']."\" class=\"btn btn-outlined btn-primary btn-md\">Click Here <br> To Book Now </a></li>";
    

    You could get crazy and use printf() as well if you like:

    $format = '<a target="_blank" href="%s" class="btn btn-outlined btn-primary btn-md">Click Here <br> To Book Now </a></li>';
    printf($format,$row['eBayURL']);
    

    If that fails, please check the value of $row['eBayURL'] like so:

    var_dump($row['eBayURL']);