08-07-2016, 05:21 AM
Hi, I am new to user spice and just started working on PhP MYSQL as i'm building a blog system. Sorry for asking stupid question. How can i fetch all data from mysql and display it? Here is the error code:
<pre></pre>
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
while($row = $stmt->fetch()){
echo '<div>';
echo '<h1>.']'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:i', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo '<p>.']Read More</p>';
echo '</div>';
}
}
?>
<pre>
Code:
Fatal error: Uncaught Error: Call to a member function query() on null
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
while($row = $stmt->fetch()){
echo '<div>';
echo '<h1>.']'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:i', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo '<p>.']Read More</p>';
echo '</div>';
}
}
?>