0

i am getting this error and was wondering how to fix it? it is when i am trying to add my articles to the feed.

class Article {
    public function fetch_all() {
        global $pdo;


        $query = $pdo->prepare("SELECT * FROM articles ORDER BY `article_id` DESC");
        $query->execute();

        return $query->fetchAll();
    }

    public function fetch_data($article_id) {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
        $query->bindValue(1, $article_id);
        $query->execute();

        return $query->fetch();
    }
}

    ?>
user2349502
  • 17
  • 2
  • 4

2 Answers2

0

$pdo is undefined by the time you are calling it. You need to create it first.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Don't use that global attempt. That's not OOP design and one of the reasons whey your code fails. Instead make sure that you have been created $pdo so far and then pass it to the constructor of Article.

Further note the capability of the so called typehints. While using them you can enforce a special type for a method param. If you pass another value at runtime (like NULL or something else) PHP will throw an error:

    typehint-----------------|
public function __construct(PDO $pdo) {
    $this->pdo = $pdo;
}

Example:

class Article {

    protected $pdo;

    // the type hint this makes !!SURE!! PDO is connected
    public function __construct(PDO $pdo) {
        $this->pdo = $pdo;
    }

    public function fetch_all() {

        $query = $this->pdo->prepare("SELECT * FROM articles ORDER BY `article_id` DESC");
        $query->execute();

        return $query->fetchAll();
    }

    public function fetch_data($article_id) {
        // note this:
        $query = $this->pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
        $query->bindValue(1, $article_id);
        $query->execute();

        return $query->fetch();
    }
}

Call it like this:

$pdo = new PDO($connectionString, $user, $pass);
$article = new Article($pdo);

var_dump($article->fetch_data($someId));
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • well i changed it to what you said then called it and i now get this error Notice: Undefined variable: connectionString in C:\xampp\htdocs\cms\header.php on line 7 Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in C:\xampp\htdocs\cms\header.php:7 Stack trace: #0 C:\xampp\htdocs\cms\header.php(7): PDO->__construct('', NULL, NULL) #1 C:\xampp\htdocs\cms\index.php(1): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\cms\header.php on line 7 – user2349502 May 04 '13 at 13:33
  • Think a moment. Are you really *unable* to answer *this* for yourself? ;) – hek2mgl May 04 '13 at 13:37
  • it cant connect to the db? – user2349502 May 04 '13 at 13:43
  • `Undefined variable: connectionString` I have used this as a placeholder as I can't know your actual settings. You'll of course have to replace it with your actual settings. Damn! ;) – hek2mgl May 04 '13 at 13:44
  • am stupid -.- lets see if i can fix it. just luring php and moved straight in to pdo :L – user2349502 May 04 '13 at 13:48
  • `and moved straight in to pdo` .. that's ok. Buy a O'Reilly!! if you are a beginner! good advice – hek2mgl May 04 '13 at 13:49
  • Believe me. Buy a PHP book (or rent some) that's the best way to get into it. you'll see (and enjoy, because seeing progress) After finishing this start your own project – hek2mgl May 04 '13 at 13:53