#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Trouble closing a DB connection in OOP


    Hello all,

    I am running into a fatal error that is baffling me and I have looked at it so much that I need a fresh set of eyes to help me find the error.

    Here is the actual error:

    Call to a member function close_conection() on a non-object in /Users/wizard1399/Sites/matthewharter.com/public/layouts/footer.php on line 10

    The first line of my php file is a require_once that directs to a config file that sets my DB params and then has a bunch of require_once calls for each of my OOP classes.

    Here is my DB class:
    class MySQLDatabase {
    //
    private $connection;
    public $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exists;
    //
    function __construct() {
    $this->open_connection();
    $this->magic_quotes_active = get_magic_quotes_gpc();
    $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
    }
    //
    public function open_connection() {
    $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if (!$this->connection) {
    die("Database connection failed: " .mysql_error() );
    } else {
    $db_select = mysql_select_db(DB_NAME);
    if (!$db_select) {
    die( "Database selection failed: " .mysql_error() );
    }
    }
    }
    //
    public function close_conection() {
    if(isset( $this->connection) ) {
    mysql_close($this->connection);
    unset($this->connection);
    }
    }
    //
    public function query($sql) {
    $this->last_query = $sql;
    $result = mysql_query($sql);
    $this->confirm_query($result);
    return $result;
    }
    //
    public function table_exists($table) {
    $tables = $this->query("SHOW TABLES LIKE '{$table}'");
    if( $this->num_rows($tables) ) {
    return true;
    } else {
    return false;
    }
    }
    //
    public function escape_value( $value ) {

    if( $this->real_escape_string_exists ){
    if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
    $value = mysql_real_escape_string( $value );
    } else {
    if( !$this->magic_quotes_active ) { $value = addcslashes( $value ); }
    }
    return $value;
    }
    //
    public function fetch_array($result_set) {
    return mysql_fetch_array($result_set);
    }
    //
    public function num_rows($result_set) {
    return mysql_num_rows($result_set);
    }
    //
    public function insert_id() {
    return mysql_insert_id($this->connection);
    }
    //
    public function affected_rows() {
    return mysql_affected_rows($this->connection);
    }
    //
    private function confirm_query($result) {
    if (!$result) {
    $output = "Database query failed: " .mysql_error() . "<br /><br />";
    $output .= "Last SQL query: " .$this->last_query;
    die( $output );
    }
    }
    }// close class
    //
    $database = new MySQLDatabase();

    I attempt to close my DB in the footer:
    <?php
    $database->close_conection();
    ?>

    As I said earlier, I have looked at this too much and can't seem to figure out how $database is not instantiated. Do I need to global $database ?
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    It's not really possible to give you an answer based on what you posted. "new" is all or nothing, it either creates the object successfully or generates a fatal error and script execution stops immediately.

    So either:
    1) $database = new MySQLDatabase(); is never run during execution of the script.
    2) You are unsetting $database at some point during the execution of the script.
    3) $database is not within the execution scope when you try to use it, which would be the case if it were called from inside a function.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo