Jump to content

PDO object not working across files


lewashby

Recommended Posts

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?

 

 

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.