|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-01-05 21:22 UTC] Ryan dot Melena at gmail dot com
Description: ------------ Provided code causes PHP error or crash when modified as discussed in comment on line 169. Reproduce code: --------------- http://66.41.135.167/phpbug/test3.phps Expected result: ---------------- IDs should list in nested order as seen when running provided code without change mentioned in comment on line 169. Actual result: -------------- Code ends prematurely with a fatal error and php crashes. MAKE SURE TO MAKE CHANGES INDICATED IN COMMENT ON LINE 169 TO REPRODUCE ERROR. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 19:00:02 2025 UTC |
<?php /* SQL CREATE TABLE IF NOT EXISTS `section` ( `id` int(10) unsigned NOT NULL auto_increment, `parentSectionID` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ; INSERT INTO `section` VALUES (1, 1); INSERT INTO `section` VALUES (2, 1); INSERT INTO `section` VALUES (3, 2); INSERT INTO `section` VALUES (4, 1); INSERT INTO `section` VALUES (5, 5); INSERT INTO `section` VALUES (6, 6); INSERT INTO `section` VALUES (7, 6); */ ?> <?php global $db_host, $db_name, $db_username, $db_password; $db_host = 'localhost'; $db_name = 'test'; $db_username = 'root'; $db_password = 'superw00t'; class section { private $id = null; private $parentSectionID = null; private $childSections = array(); private $status = array('consistentWithDb' => false, 'childSectionsLoaded' => false); function __construct($id) { $db = resourceHelper::getDbConn(); // Validate $id and load section data if(!is_int($id)) { throw new Exception('Invalid argument for section::loadFromDb(), $id must be an int. Value sent was [' . $id . '] of type [' . gettype($id) . '].'); } $this->id = $id; $query = 'SELECT parentSectionID FROM section WHERE id = :id'; $stmt = $db->prepare($query); $stmt->bindValue(':id', $this->id , PDO::PARAM_INT); $stmt->execute(); $stmt->bindColumn('parentSectionID', $this->parentSectionID, PDO::PARAM_INT); $stmt->fetch(PDO::FETCH_BOUND); $stmt = null; $db = null; } function __get($varName) { switch($varName) { case 'id': return (int)$this->id; case 'childSections': if(!$this->status['childSectionsLoaded']) { $this->loadChildSections(); } return $this->childSections; default: throw new Exception('Variable [' . $varName . '] is not a public member of section.'); break; } } private function loadChildSections() { $sectionID = null; $db = resourceHelper::getDbConn(); $query = 'SELECT id FROM section WHERE parentSectionID = :id AND parentSectionID != id'; $stmt = $db->prepare($query); $stmt->bindValue(':id', $this->id, PDO::PARAM_INT); $stmt->execute(); $stmt->bindColumn('id', $sectionID, PDO::PARAM_INT); while($stmt->fetch(PDO::FETCH_BOUND)) { $this->childSections[] = new section($sectionID); } $this->status['childSectionsLoaded'] = true; } } class resourceHelper { public static function getDbConn() { global $db_host, $db_name, $db_username, $db_password; // Create and Validate database connection try { $db = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name, $db_username, $db_password); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(Exception $e) { throw new Exception('Unable to connect to database [' . $config['db']['name'] . '] using configuration file values.' . $e->getMessage()); } return $db; } } $db = resourceHelper::getDbConn(); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sectionID = null; $query = 'SELECT id FROM section WHERE id = parentSectionID'; $stmt = $db->prepare($query); $stmt->execute(); $stmt->bindColumn('id', $sectionID, PDO::PARAM_INT); while($stmt->fetch(PDO::FETCH_BOUND)) { $section = new section($sectionID); buildSectionRow($section); } $stmt = null; $db = null; function buildSectionRow(section $section, $indent = 0) { echo $section->id . '<br />'; $indent++; /* To reproduce error, comment out $test definition then replace $test in foreach loop with $section->childSections */ $test = $section->childSections; foreach($test as $childSection) { buildSectionRow($childSection, $indent); } } ?>