Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I ran into an error which I have trouble explaining, and it's probably due to my lack of understanding of how variables work in PHP. Whenever I run my web-app, I get the error that I am trying to execute a method on a non-object, but the parameter has previously been assigned to an object as far as I know.

class PSQLDB implements IDatabase
{
    public $DB;

    public function __construct($dbConfig)
    {
        $connString = $dbConfig['driver'] . ':';
        foreach ($dbConfig['properties'] as $key => $value)
        {
            $connString .= $key . '=' . $value . ';';
        }
        try
        {
            $this->DB = new PDO($connString, $dbConfig['username'], $dbConfig['password']);
            $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (Exception $ex)
        {
            error_log($ex->getMessage());
        }
    }

    public function getEvents()
    {
        // TODO: Implement getEvents() method.
    }

    public function getRecipes()
    {
        $query = "SELECT * FROM RECIPES";
        $statement = null;
        $statement = $this->DB->prepare($query);

That's the code snippet on which the last line says that DB is not an object, but if I'm correct, should it not be an object because we set it to one in the constructor?

I'm a bit lost here, can anyone explain why the last line says that DB is not a variable, even though the method in the constructor executed the setAttribute method without trouble? And, more importantly, how can I make sure that he still refers to the object in the other method?

share|improve this question

closed as off-topic by Snowman, MichaelT, GlenH7, Bart van Ingen Schenau, gnat Mar 9 at 20:58

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What do you use to develop? I am hoping that it is an IDE, not just a plain text editor. Ste through your code in the debugger and the problem should be apparent. I would expect to find it in less time than it took to post this question. –  Mawg Mar 8 at 7:21
    
Failure to migrate indicates possibility of question block at SO. Do not post off-topic content on a site in an attempt to circumvent a question block on another site. This is not an appropriate question here - if you wish to get out of the block on Stack Overflow, please read the linked article that appears on the block message. You know, this one. –  gnat Mar 9 at 20:59

2 Answers 2

Your code appears to be correct. Check your error log. I'm guessing that your connect is failing which throws an exception which you are then silently capturing. So your DB->setAttribute() is never executed.

Better to rethrow the exception or at least stop execution with an explicit error message.

share|improve this answer

Okay guys so the problem was not within my code at all, but rather due to the configuration of the WAMP server on which it was running, which did not allow a connection to the database to be established properly.(Even though the right drivers should have been enabled). Reinstalled the server, added the right drivers and everything worked without a problem.

share|improve this answer
1  
Which is great but your posted code still does not handle failed connections properly. Try changing the password(for example) in your connect string. You will be back to square one. –  Cerad Mar 8 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.