3

Been trying to figure this out for a couple of days now. Havn't come up with any solution and I've searched and asked friends about it. But with no good results.

When I user $db->query(); It gives me this error: Fatal error: Call to a member function fetch() on a non-object

And when I use prepare, I get no result at all, just a blank page.

<?php
$query = $dbh->query("SELECT name, ad_offer, content, expiration_date FROM biz_ads ORDER BY id DESC LIMIT 0,5");
while($row = $query->fetch(PDO::FETCH_NUM)) {
        $name = $row[0];
        $ad_offer = $row[1];
        $content = $row[2];
        $expiration_date = $row[3];
        ?>
        <div class="name"> 
        <?php 
        echo $name;
        ?>
        <div class="ad_offer">
        <?php
        echo $ad_offer;
        ?>
        </div>
        <div class="content">
        <?php
        echo $content;
        ?>
        </div>
        <div class="expiration_date">
        <?php
        echo $expiration_date;
        ?>
        </div>
    <?php
        }
?>

Any help is appreciated. The weird thing is, that I actually got a old similar query to work not long ago at all. Still not working.

<?php

$hostname = 'localhost';
$username = 'root';
$password = '';

$dbh = new PDO("mysql:host=$hostname;dbname=broet;", $username, $password);

?>
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user2328659
  • 71
  • 1
  • 6

1 Answers1

0
$query = "SELECT name, ad_offer, content, expiration_date 
         FROM biz_ads 
         ORDER BY id DESC 
         LIMIT 0,5";
$stmt = $dbh->prepare($query);
$stmt->execute()or die("error");
Lance
  • 4,736
  • 16
  • 53
  • 90