Eggzorcist Posted January 1, 2011 Share Posted January 1, 2011 I made a small script and it says that my database object isn't an object. I connect to my pdo database via: <?php try { $db = new PDO("mysql:host=localhost;dbname=database", "root", "root"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $e->getMessage(); } ?> The error I get is Notice: Undefined variable: db in /Users/JPFoster/Sites/jobiji_sandbox/extensions/tasks.php on line 21 Fatal error: Call to a member function query() on a non-object in /Users/JPFoster/Sites/jobiji_sandbox/extensions/tasks.php on line 21 here's where the error is $grabber = $db->query('SELECT * WHERE user_id =' . $userid); $grabber->setFetchMode(PDO::FETCH_OBJ); while ( $row = $grabber->fetch() ){ $display = '<div>Task Name: ' . $row->name .'</div>'; $display .= '<div>Task Details: <p>'. $row->details .'</p></div>'; //$display .= '<div>'. $this->checkifDone($row->task_status) .'</div>'; $display .= '<div>Task Due: '. $row->task_due . '</div>'; echo $display; } could anyone tell me where this issue is originating I followed a fairly simple tutorial that didn't seem to work. Thanks! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2011 Share Posted January 1, 2011 It would really help if you showed the code between those two points. If $db is not defined, you are either un-setting it or you have a variable scope problem. Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted January 1, 2011 Author Share Posted January 1, 2011 here is my class Its probably a variable scope issue, how do I go to fix this? Must I put a PDO object in all my objects that will use the database? class fetcher { function checkifDone( $value ){ if ( $value == 0 ) { echo 'Not Done'; } elseif ( $value == 1 ){ echo 'Done'; } } function grab_tasks( $userid ){ $grabber = $db->query('SELECT * WHERE user_id =' . $userid); $grabber->setFetchMode(PDO::FETCH_OBJ); while ( $row = $grabber->fetch() ){ $display = '<div>Task Name: ' . $row->name .'</div>'; $display .= '<div>Task Details: <p>'. $row->details .'</p></div>'; //$display .= '<div>'. $this->checkifDone($row->task_status) .'</div>'; $display .= '<div>Task Due: '. $row->task_due . '</div>'; echo $display; } } } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2011 Share Posted January 1, 2011 You would generally just pass the $db object into your class when you create an instance of your class. In your class constructor, store it in a class variable that you can then reference in any of the methods in your class - $what_ever = new fetcher($db); // pass the db object into the instance of your class. Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted January 1, 2011 Author Share Posted January 1, 2011 Do I have to catch the variable in my object somehow? I tried adding the variable in the object creation parameter but I still get the same errors. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2011 Share Posted January 1, 2011 Someone already stated you would need to store it in a class variable - In your class constructor, store it in a class variable that you can then reference in any of the methods in your class Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted January 2, 2011 Author Share Posted January 2, 2011 I'm a little bit confused about how I should reference it in my __contruct() from simply passing it on in the new fetcher() parameters. Its the first time I use PDO in an object and I'm wondering what is the most ideal and practical method in doing this correctly. Thank you very much! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2011 Share Posted January 2, 2011 Setting and referencing class variables are some of the basics you should know before attempting to use a class. This has nothing to do with using PDO in an object - <?php class fetcher { private $pdo; // class variable for PDO object function __construct($db){ $this->pdo = $db; // save the passed $db parameter into the class variable } function grab_tasks($userid){ $grabber = $this->pdo->query('SELECT * WHERE user_id =' . $userid); // reference the ->query() method of the class variable .... } } // instance of your db class $db = new PDO("mysql:host=localhost;dbname=database", "root", "root"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // instance of your class $what_ever = new fetcher($db); // pass the db object into the instance of your class. ?> Quote Link to comment Share on other sites More sharing options...
Eggzorcist Posted January 3, 2011 Author Share Posted January 3, 2011 I made the changes and I now understand the double referencing. However it still says that the query part of it is a non-object. How is this so? Fatal error: Call to a member function query() on a non-object in .../extensions/tasks.php on line 24 Here is my updated php code: <?php try { $db = new PDO("mysql:host=localhost;dbname=dbsanbox", "root", "root"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $e->getMessage(); } $userval = '2'; class fetcher { private $pdo; function __contruct($db){ $this->pdo = $db; } function checkifDone( $value ){ if ( $value == 0 ) { echo 'Not Done'; } elseif ( $value == 1 ){ echo 'Done'; } } function grab_tasks( $userid ){ $grabber = $this->pdo->query('SELECT * WHERE user_id =' . $userid); $grabber->setFetchMode(PDO::FETCH_OBJ); while ( $row = $grabber->fetch() ){ $display = '<div>Task Name: ' . $row->name .'</div>'; $display .= '<div>Task Details: <p>'. $row->details .'</p></div>'; //$display .= '<div>'. $this->checkifDone($row->task_status) .'</div>'; $display .= '<div>Task Due: '. $row->task_due . '</div>'; echo $display; } } } $fetcher = new fetcher($db); $fetcher->grab_tasks($userval); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2011 Share Posted January 3, 2011 You managed to miss-spell construct Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.