lewashby Posted December 20, 2015 Share Posted December 20, 2015 I'm getting the following errors when I run `cat /var/log/apache/error.log` -> PHP Notice: Undefined variable: db_connection in /var/www/html/popreport/includes/inmate.php on line 18 -> PHP Fatal error: Call to a member function query() on a non-object in /var/www/html/popreport/includes/inmate.php on line 18 When I try this in my browser I start with test.php test.php <?php require_once("./database.php"); require_once("./inmate.php"); // foreach($query as $row) // { // print_r($row) . "<br />"; // } $inmate = array(); $inmate = new Inmate($inmate); foreach($inmate as $row) { print $row->firstl_name . "<br />"; } ?> database.php <?php include("./constants.php"); try { $db_connection = new PDO("mysql:host=$host;dbname=$db_name", $db_user, $password); $db_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br />"; die(); } ?> inmate.php <?php require_once("./database.php"); class Inmate { private $first_name = ''; private $last_name = ''; private $full_name = ''; private $race = ''; private $number = 0; private $facility = ''; private $type_of_transit = ''; public function __construct($inmate) { $sql = "SELECT * FROM inmate_board"; $query = $db_connection->query($sql); $result = $query->fetch(PDO::FETCH_ASSOC); foreach($result as $row) { $this->$first_name = $result['first_name']; } } public function get_property($property) { return $this->$property; } } ?> In inmate.php I also tried to change the line `$query = $db_connection->query($sql);` to `$query = global $db_connection->query($sql);` but I didn't have any luck here either. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/299877-pdo-object-not-working-across-files/ Share on other sites More sharing options...
mac_gyver Posted December 20, 2015 Share Posted December 20, 2015 if your Inmate class is DEPENDENT on using a database class, you should use dependency injection (a web search will explain what that means) to get the instance of the pdo database class into your Inmate class. usually, you would supply the instance of the pdo database class as a call time parameter when you initiate the Inmate class. you would store the instance of the pdo database class in a property in your Inmate class and reference it within the class wherever it is need. why are you passing an empty array as input into your Inmate class when you initiate it, but are not using that array in the constructor method code? this is not how classes are used. when you create an instance of a class, you reference the class methods and class properties of that class. any input data from the calling code would be passed into a method as a call time parameter and any output data back to the calling code would be returned from a method. Quote Link to comment https://forums.phpfreaks.com/topic/299877-pdo-object-not-working-across-files/#findComment-1528258 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.