Jack Partington
Jack Partington

Reputation: 65

No output on webpage from the database

When loading my home.php page it should print the data from the select the data from the database 'blog_post' onto the page. However the record gets inserted properly but no data is shown on the page. I have looked up various solutions on here and none of them have seemed to work.

I have looked at many questions on here similar to my problem. None of the answers have seemed to answer my issue. I had tried this using PDO which had worked but with tables, however I do not want to do it that way as it was also a bit much for what I was trying to achieve.

$stmt = $con->prepare("SELECT postTitle, postCont FROM blog_post WHERE 
blogid = ? ORDER BY blogid DESC");
$stmt->bind_param('s', $postTitle['postTitle']);
$stmt->execute();
$stmt->bind_result($postTitle, $postCont);
$stmt->fetch();
if(!$postTitle){
    echo "<p align='center'>No Blog Currently Available!</p>";
} else {
    echo '<div>';
    echo '<h1>'.var_dump($postTitle).'</h1>';
    echo '<p>'.$postCont.'</p>';                
    echo '</div>';
}

I expect that when the data is added to the database via my admin.php page. It should then display the data onto home.php.

Upvotes: 0

Views: 83

Answers (1)

Dharman
Dharman

Reputation: 33242

fetch returns Boolean not an array. You've bound two variables, so use them:

$stmt->bind_result($postTitle, $postCont);
$stmt->fetch();
if(!$postTitle){
    echo "<p align='center'>No Blog Currently Available!</p>";
} else {
    echo '<div>';
    echo '<h1>'.$postTitle.'</h1>';
    echo '<p>'.$postCont.'</p>';                
    echo '</div>';
}

Upvotes: 2

Related Questions