Jump to content

PDO $db object "non-object" error?


Eggzorcist

Recommended Posts

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!

 

Link to comment
Share on other sites

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;
	}
}

}

 

 

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

?>

Link to comment
Share on other sites

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);


?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.