Jump to content

[SOLVED] Construct function throwing errors


bdmovies

Recommended Posts

<?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....
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.