0

I have been trying to pull all infomation where $user is equal to subscriberID in my sql database. I am using the PDO method of inserting data and wish to use the query function also. i am getting the following error message back from my try condition.

Fatal error: Call to a member function setFetchMode() on a non-object

I have considered maybe the problem has been caused by the method i use to pull the data as im fairly new to PDO.(i include a top.php that establishes the link to the database)

Thank you for your suggestions

 <?php
 include "top.php";

    try{
        $user = $_SESSION['login'];
        echo "Welcome <strong>$user</strong> to your saved holidays";

        //$getSavedHolidays = $db->query("SELECT * FROM saved_holidays WHERE subscriberID='$user'");
        //preparing a PDO statment for accessing saved holidays when the logged in user matchs the subscriberID
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $getSavedHolidays = $db->prepare("SELECT * FROM saved_holidays WHERE subscriberID=:user");
        //uses a bind valu as this makes the statment more secure
        $getSavedHolidays->bindValue(":user", $_SESSION['login']);
        $getsavedHolidays->setFetchMode(PDO::FETCH_ASSOC);
        $getSavedHolidays->execute();


        foreach ($Result as $row)
            {
                echo "<p><a href'".$row['link']."'>".$row['title']."</a><br \>" . 
                     $row['description'] . "<br \>" . 
                     $row['pubDate'] ."<br \></p>";
            }

        }

        catch (PDOException $e) 
            {
                 print_r($e->errorInfo);
                    die();
            }

?>

HxM Graeme
  • 117
  • 1
  • 3
  • 12
  • You have an error: `$$getSavedHolidays = $db->prepare("SELECT * FROM saved_holidays WHERE subscriberID=:user");` should be `$getSavedHolidays` (and not `$$`). – Madara's Ghost May 02 '12 at 15:30

2 Answers2

4

You should be preparing your statement. It has a nice added bonus of being more secure, plus it will solve your problem.

$user = $_SESSION['login'];

try {
    $db = new PDO("mysql:host=localhost;dbname=database", "user", "pass");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $getSavedHolidays = $db->prepare("SELECT * FROM saved_holidays WHERE subscriberID=:user");
    $getSavedHolidays->bindValue(":user", $_SESSION['login']);
    $getSavedHolidays->setFetchMode(PDO::FETCH_ASSOC);
    $getSavedHolidays->execute();
    $Result = $getSavedHolidays->fetchAll();

    foreach ($Result as $row) {/*...*/}

}
catch (PDOException $e) {
    print_r($e->errorInfo);
    die();
}

EDIT

You have an error in the code you've pasted above:

$$getSavedHolidays = $db->prepare("SELECT * FROM saved_holidays WHERE subscriberID=:user");

Should be

$getSavedHolidays = $db->prepare("SELECT * FROM saved_holidays WHERE subscriberID=:user");
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
2

there is explicit syntax error. mussing closing single quote:

$getSavedHolidays = $db->query("SELECT * FROM saved_holidays WHERE subscriberID='$user'");

and by the way, since you're using PDO it's more safe to use bindValue function to pass values to SQL request.

heximal
  • 10,327
  • 5
  • 46
  • 69
  • thanks i cant believe i missed it i was looking over it for a good half hour. Fatal error: Call to a member function setFetchMode() on a non-object is the new error is this due to my fetch method – HxM Graeme May 02 '12 at 15:07