bdmovies Posted April 16, 2009 Share Posted April 16, 2009 <?php require_once ('classes/database.class.php'); class cases extends database { function __construct($caseID) { # Get the case details $sql = "SELECT * FROM cases WHERE caseID = '$caseID'"; $result = $this->query($sql); $row = $this->fetchArray($result); foreach($row as $key=>$value) { $this->$key = $value; } } }?> ...Throws this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /*/classes/database.class.php on line 52 Warning: Invalid argument supplied for foreach() in /*/classes/cases.class.php on line 11 Notice: Undefined property: cases::$plaintiff in /*/test.php on line 11 vs. Notice: Undefined property: cases::$defendant in /*/test.php on line 13 However, This DOES work: <?php class cases extends database { function getCaseInfo($caseID) { # Get the case details $sql = "SELECT * FROM cases WHERE caseID = '$caseID'"; $result = $this->query($sql); $row = $this->fetchArray($result); foreach($row as $key=>$value) { $this->$key = $value; } } } ?> Test.php looks like this; <?php /** * Test file */ error_reporting(E_ALL); // Get required files require_once 'classes/cases.class.php'; $T = new cases('3'); echo $T->plaintiff; echo "<br/>vs.<br/>"; echo $T->defendant; /* Or */ $T = new cases(); $info = $T->getCaseInfo('3'); echo $T->plaintiff.... ?> Quote Link to comment https://forums.phpfreaks.com/topic/154285-solved-construct-function-throwing-errors/ Share on other sites More sharing options...
rhodesa Posted April 16, 2009 Share Posted April 16, 2009 does the database class have a __constuct function that needs to be run? if so, your non-working copy is not running it. try updating it to this: <?php require_once ('classes/database.class.php'); class cases extends database { function __construct($caseID) { # Run parent parent::__construct(); # Get the case details $sql = "SELECT * FROM cases WHERE caseID = '$caseID'"; $result = $this->query($sql); $row = $this->fetchArray($result); foreach($row as $key=>$value) { $this->$key = $value; } } }?> Quote Link to comment https://forums.phpfreaks.com/topic/154285-solved-construct-function-throwing-errors/#findComment-811120 Share on other sites More sharing options...
bdmovies Posted April 16, 2009 Author Share Posted April 16, 2009 Perfect! Thanks a lot, I knew it was something small. I was poking around the php site and was thinking it had to be something with a parent/child relationship. Quote Link to comment https://forums.phpfreaks.com/topic/154285-solved-construct-function-throwing-errors/#findComment-811123 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.