Re: Does PDO prevent class functions from calling other class functions?
| From: | Ken Tozier | Date: | Thu, 02 Aug 2007 16:28:18 +0000 |
| Subject: | Re: Does PDO prevent class functions from calling other class functions? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260071@lists.php.net to get a copy of this message | ||
On Aug 2, 2007, at 12:19 PM, Nathan Nobbe wrote:
On 8/2/07, Ken Tozier <kentozier@comcast.net> wrote:
How would I go about that? I just took a quick look at the try/catch
documentation and it looks like it's my responsibility to throw
exceptions which isn't particularly useful if it's PHP itself that is
doing something unexpected....
well, youre not throwing an exception here in your code, your doing something called
swallowing the exception. which is typically not a good practice.
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
and you could show us the output from
$e->getMessage()
thats where the script is blowing up. calling die() after that will obviously stop processing
so you have no hope of recovering gracefully.
There is no error. The browser calls the script and paints a blank screen. If I do a "view source" same deal, nothing.
Here's the MySQLDatabase "query_database' function that passes the actual call to PDO
function query_database($inQuery)
{
$query = $inQuery;
$coersions = null;
$object_key = null;
$group_by_key = null;
if (is_array($inQuery))
{
$query = $inQuery['query'];
$coersions = $inQuery['coersions'];
$object_key = $inQuery['object_key'];
$group_by_key = $inQuery['group_by_key'];
}
try
{
// determine query type
if (strpos($query, 'insert') === false)
{
$rows = array();
$rowCounter = 0;
foreach ($this->db->query($query, PDO::FETCH_NAMED) as $row)
{
$rowFields = array();
$recordKey = $rowCounter;
foreach ($row as $key => $value)
{
// remember this key if it matches the user specified object key
if (($object_key != null) && ($key == $object_key))
$recordKey = $value;
// perform user specified coersions
if ($coersions != null)
$value = $this->coerce_value($value, $coersions[$key]);
$rowFields[$key] = $value;
}
// perform grouping if requested
if ($group_by_key == null)
$rows[$recordKey] = $rowFields;
else
{
$groupKey = $rowFields[$group_by_key];
if ($rows[$groupKey] == null)
$rows[$groupKey] = array();
$rows[$groupKey][] = $rowFields;
}
$rowCounter++;
}
return $rows;
}
else
{
// return last insert ID
return ($this->db->lastInsertId() + 0);
}
}
catch (PDOException $error)
{
print "Error!: " . $error->getMessage() . "<br/>";
//die();
}
}
And here's a modified version of the "directory_path_for_pub_id" with a try/catch
function directory_path_for_pub_id($inPubID, $inDirType)
{
echo 'entered: directory_path_for_pub_id';
try
{
$site = $this->site_for_pub_id($inPubID);
echo 'site: '.$site.'<br>';
}
// Tried both of these. Result: blank window/blank "view source'
catch ($e)
catch (Exception $e)
{
print $e.'<br>';
}
}
-nathan On 8/2/07, Ken Tozier <kentozier@comcast.net> wrote: On Aug 2, 2007, at 12:03 PM, Nathan Nobbe wrote: can you supply an error or warning that php is giving ? it would help to determine the problem. How would I go about that? I just took a quick look at the try/catch documentation and it looks like it's my responsibility to throw exceptions which isn't particularly useful if it's PHP itself that is doing something unexpected.... http://www.php.net/manual/en/language.exceptions.php -nathan On 8/2/07, Ken Tozier <kentozier@comcast.net> wrote: Hi I've been writing PHP classes for around two years now but all of a sudden, the things I used to do don't work any more. I have a class that implements utility functions for database calls using PDO and am finding that I can't call one utility function from within another. If each function is called by itself from a script, they work perfectly. This is really basic stuff so I'm very puzzled why it isn't working. The only difference between these new classes and my old classes is the use of PDO. Could PDO be causing all these headaches? Anyone see where I'm screwing up? Thanks in advance Ken Here's the '__construct' function of the included 'MySQLDatabase' class function __construct($inDomain, $inUser, $inPassword, $inDBName) {try { $this->db = new PDO('mysql:host='.$inDomain.';dbname='.$inDBName, $inUser, $inPassword);// set error reporting $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);} catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }} Here's the utility class so far <?php// MySQLDatabase is a DB access class that handles a bunchof stuff like pulling values out of a query// value coersion etc. I've tested this for the last day orso and it seems to be stable.include_once('MySQLDatabase.php');class PMXUtilities { private $db;function __construct() { $domain = 'localhost'; $user = 'root'; $password = ''; $db_name = 'pagemanager';$this->db = new MySQLDatabase($domain, $user, $password, $db_name);}// if this is called on its own, it works function site_for_pub($inPubID) { $query = 'selectsite.id, site.site_name from site, publication where publication.id='.$inPubID.' and publication.site=site.id';$coersions = array('id'=> 'integer');$args = array('query'=> $query, 'coersions'=> $coersions);$queryResult = $this->db-query_database($args);
if (count($queryResult) > 0)
return $queryResult[0]['id'];
else
return 'Error:
PMXUtilities.site_for_pub_id failed while fetching
site info for publication:'.$inPubID;
}
/ if this is called on its own, it works
function directory_type_id_for_name($inName)
{
$query = 'select
id from directory_type where name="'.$inName.'"';
$coersions = array('id'=> 'integer');
$args = array('query'=> $query, 'coersions'=> $coersions);
$queryResult = $this->db-query_database($args);
if (count($queryResult) > 0)
return $queryResult[0]['id'];
else
return 'Error:
PMXUtilities.site_for_pub_id failed while fetching
directory type id for directory:'.$inName;
}
// this function never gets past the first "echo"
function directory_path_for_pub_id($inPubID,
$inDirType)
{
echo 'entered: directory_path_for_pub_id';
// seems to die on next line as the 'echosite' line never prints
$site = $this-site_for_pub_id($inPubID);
echo 'site: '.$site.'<br>';
/*
$dirTypeID = $this-
directory_type_id_for_name($inDirType);
echo 'dir_type: '.$dirTypeID.'<br>';
$query = "selectserver.server_name, directory.path from directory, server, site where directory.type=".$dirTypeID." and directory.server=server.id and server.site=site.id and site.id=". $site;
echo $query.'<br>';
$queryResult = $this->db->query_database
($query);
return $queryResult[0]->server_name.$queryResult[0]->path;
*/
}
}?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php