Forum Moderators: coopster

Message Too Old, No Replies

PDO/MySQL debug assist requested

Why won't this return the requested data

         

laidbackwebsage

8:26 pm on Mar 4, 2009 (gmt 0)

10+ Year Member



Problem: I have verified that the PDO object is valid and working. But when the object is instantiated, the "hierarchies" array in the class is not populated.

The exact code provided below works if implemented in a stand-alone script; but it will not work from the class.

Additionally, it will not print the row, nor output to the error log as requested in the " setHierarchies()" function. ("error_log" is on in the php.ini file.)

Can anyone give me some suggestions on why?

Thanks in advance!

Kevin J


<?php
class Hierarchies
{
/// Member Variables
protected $dbh=NULL;
protected $hierarchies=array();

/// Member Methods
public function _construct( $incoming_dbh = NULL )
{
$this->setDbHandle( $incoming_dbh );

/// Set the list of hierarchies
$this->setHierarchies();
return $this;
}

protected function setDbHandle( $incoming_dbh )
{
$this->dbh=$incoming_dbh;
return TRUE;
}

public function getDbHandle() { return $this->dbh; }

protected function setHierarchies()
{
$sql="SELECT hierarchy_guid, hierarchy_name FROM hierarchies ORDER BY hierarchy_name ASC";
foreach ($this->getDbHandle()->query( $sql ) as $row)
{
print_r( $row );
error_log( print_r( $row ) );
$this->hierarchies[ $row['hierarchy_guid'] ] = $row['hierarchy_name'];
}
return TRUE;
}

public function getHierarchies() { return $this->hierarchies; }
}
?>

dreamcatcher

8:30 am on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi laidbackwebsage,

Try setting your error reporting to report ALL errors. This might throw a clue as to what the problem is.

dc

blang

3:05 am on Mar 12, 2009 (gmt 0)

10+ Year Member



A constructor cannot return a value, let alone the object instance reference.


foreach ($this->getDbHandle()->query( $sql ) as $row)

Why do you have the method getDbHandle() involved here? Why not just reference $this->dbh? Really, I see no reason to have a getter or setter method for the PDO handle, as you should internalize it (as you've done with $this->dbh) and not allow anyone to set or retrieve that handler.

Furthermore, your setHierarchies() method returns TRUE, regardless of what happens. You should either have it return the array, or a logic based boolean value (i.e. TRUE on success, FALSE on failure).

I personally think you're overthinking the concept. A single static method that takes the PDO handler as an argument, and produces the hierarchies array is all you need.