I'm trying to fetch data from my MySQL database and display it on a simple webpage.
However when i add the PHP code to the .html file it almost looks like the code isn't parsed as PHP.
The code below connects to the db (which works), I enter a query and then fetch the results.
However given the output it almost looks like the ->
in $conn->query($query)
is enterpreted as ?>
The page resulting from the code below shows these three lines of text:
query($query)) {while ($row = $result->fetch_assoc()) {echo '
'.$row['Title'].'
';>$result->free();}else"fail";}?>
Which is in accordance to the format i want, but obviously not what intended.
However i get the same resulting page from this code when i remove everything from the second line till $conn->
.
Which gives me the impression that the code followed by $conn->
isn't read as PHP code at all.
I think it's also important to note that i don't have this problem in a different part of my page, i.e. creating a database connection works fine and code followed by the query is parsed as i would expect.
Is my assumption correct and how can i counter this? Or have a missed something obvious causing this error?
<div class="col-sm-8 blog-main">
<?php
$servername = "localhost";
$username = "root";
$password = "marvin";
$dbname = "blogpostdb";
$conn = new mysqli($servername, $username, $password, $dbname);
$query = "SELECT * FROM posts";
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
echo '<div class="blog-post">
<h2 class="blog-post-title">'. $row['Title'] .'</h2></div>';
}
$result->free();
}
else{
echo "fail";
}
?>
</div><!-- /.blog-main -->