0

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.

12
  • 4
    Are you sure that your database column blogid shold be compared with the bound value of $postTitle? Commented Sep 5, 2019 at 9:25
  • 2
    "no data is shown on the page" ...do you mean that you see the "No Blog Currently Available!" message, or that you literally see nothing at all? If the latter, please check if PHP crashed, and whether error reporting / logging is enabled. Then you might be able to see if an error occurred. I suspect you might have one because $row is not an array. As per the docs, fetch() can only return true, false or null. When you work with fetch(), you get the data by using bind_result, not by reading row arrays. Commented Sep 5, 2019 at 9:44
  • I see "No Blog Currently Available!" on the page, even though there is data in the database. When using an array before (forgot to post that in the question), it had another error (which is can't quite remember now). I believe it was something to do with being false. Commented Sep 5, 2019 at 9:48
  • 1
    Ok well like I said, that'll be because $row isn't an array. Probably a warning is being generated as well (but possibly suppressed). You've bound your output variables to $postTitle and $postCont, so use them. echo '<h1>'.$postTitle.'</h1>'; echo '<p>'.$postCont.'</p>';. I think you've got confused between the syntax you're using now and the syntax you can use when you do it via get_result() Commented Sep 5, 2019 at 9:51
  • 1
    P.S. Pay attention to the very first comment as well - unless you've named your variables very badly, you appear to have a mismatch which might lead to no results being returned anyway. It might even generate an error if $postTitle contains a string, since you've told the database to expect an integer. Commented Sep 5, 2019 at 9:51

1 Answer 1

2

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>';
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.