Home > Net >  fetching data from database is not working when theres class or id
fetching data from database is not working when theres class or id

Time:05-21

I can fetch the data from the database no problem but when I put back the class in it, it doesn't work anymore and I'm getting an error 500. Can somebody guide me on how can I fix this I would really appreciate it.

<?php

$sql = "SELECT name, lineage1, lineage2  FROM cultivar_db WHERE name = '1haze'";
$stmt = mysqli_stmt_init($connect);

if (!mysqli_stmt_prepare($stmt, $sql)){
echo "FAILED TO CONNECT!";
} else {
    
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    while ($row = mysqli_fetch_assoc($result)){
        echo $row['name']. "<p >". $row['lineage1']. "<b>X</b>". $row['lineage2']. "</p>";

    }
}
  
?>

CodePudding user response:

This is because you are not escaping the quoted " inside of the HTML

change

echo $row['cultivar_name']. "<p >". $row['lineage1']. "<b>X</b>". $row['lineage2']. "</p>";

to and replace SOMECLASS with your class

echo $row['cultivar_name']. "<p class=\"SOMECLASS\">". $row['lineage1']. "<b>X</b>". $row['lineage2']. "</p>";

Note how I escaped the " with a backslash

"<p class=\"SOMECLASS\">"

you could also do single quotes if you don't want to escape.

"<p class='SOMECLASS'>"

Note: When writing double quotes in double quotes, and single quotes inside of single quotes, you must escape them with a backslash, you could alternatively use single quotes inside of double quotes and vice versa.

  • Related