-1

Possible Duplicate:
Scope error - Call to a member function prepare() on a non-object

I wrote this code about 2 or 3 months ago on my windows PC. It runs.. Now I downloaded xampp on my Mac and I get the following error:

Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/connect/includes/functions.php on line 39

// Connect to the database
    try {
        $pdo = new PDO('mysql:host=localhost;dbname=mo33', 'root', '');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }catch (PDOException $e){
        $error = "There was an issue connecting to the database: \n" + $e->getMessage();
        include 'html/error.html.php';
        exit();
    }

function renderTimeline(){

        try {
            $sql = 'SELECT user_publications.id, user.firstname, user.lastname, user.thumbnail, article, date_added
                    FROM user_publications INNER JOIN user 
            ON user_id = user.id 
            ORDER BY user_publications.id DESC';
            $s = $pdo->prepare($sql);   ---------------------------LINE 39--------------- 
            $result = $pdo->query($sql);
        }catch(PDOException $e){
            $output = 'There was an error while finding posts in the database..: ' . $e->getMessage();
            include 'html/error.html.php';
            exit();
        }

        while($row = $result->fetch()) {
            $user_publications[] = array(
        'id'=>$row['id'], 
        'name'=>$row['firstname'] + " " + $row['lastname'], 
        'thumbnail'=>$row['thumbnail'], 
        'article'=>$row['article'], 
        'date'=>$row['date_added']);
        }
        foreach ($user_publications as $post) {
            renderUserPublication($post['id'], $post['name'], $post['thumbnail'], $post['article'], $post['date']);
        }

        return;
    }
Community
  • 1
  • 1

1 Answers1

0

pass the object too your function like so

function renderTimeline($pdo){

and in your code

$pdo = new pdo();
renderTimeline($pdo);
Hydra IO
  • 1,537
  • 1
  • 13
  • 28