Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600090
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:27:03+00:00 2026-05-18T20:27:03+00:00

Ok, messing about with classes in PHP and can’t get it to work the

  • 0

Ok, messing about with classes in PHP and can’t get it to work the way I’m used to as a C++/Java-guy. In the “_init” function, if I run a query at the “// query works here” line”, everythong works, but in the “getUserID” function, all that happens is said warning…

“getUserID” gets called from login.php (they are in the same dir):

login.php

<?php
include_once 'sitehandler.php';
include_once 'dbhandler.php';
session_start();

#TODO: Safer input handling
$t_userName = $_POST["name"];
$t_userId = $_SESSION['handler']['db']->getUserID($t_userName);

if ($t_userId != -1) {
    $_SESSION['user']['name'] = $t_userName;
    $_SESSION['user']['id'] = $t_userId;
}

//error_log("user: " . $_SESSION['user']['name'] . ", id: ". $_SESSION['user']['id']);
header("Location: " . $_SERVER["HTTP_REFERER"]);
?>

dbhandler.php

 <?php
include_once 'handler.php'; 

class DBHandler extends HandlerAbstract {
private $m_handle;

function __construct() {
    parent::__construct();
}

public function test() {
    #TODO: isdir liquibase
    #TODO: isfile liquibase-195/liquibase + .bat + execrights
    $this->m_isTested = true;
}

public function _init() {
    if (!$this->isTested()) $this->test();
    if (!file_exists('files/data.db')) {
        #TODO: How to to if host is Windows based?
        exec('./files/liquibase-1.9.5/liquibase --driver=org.sqlite.JDBC --changeLogFile=files/data_db.xml --url=jdbc:sqlite:files/data.db update');
        #TODO: quit if not success
    }

    #TODO: Set with default data
    try {
        $this->m_handle = new SQLite3('files/data.db');
    } catch (Exception $e) {
        die("<hr />" . $e->getMessage() . "<hr />");
    }

    // query works here
    $this->m_isSetup = true;
}

public function teardown() {

}

public function getUserID($name) {
    //  PHP Warning:  SQLite3::prepare(): The SQLite3 object has not been correctly initialised in
    $t_statement = $this->m_handle->prepare("SELECT id FROM users WHERE name = :name");
    $t_statement->bindValue(":name", $name, SQLITE3_TEXT);
    $t_result = $t_statement->execute();
        //var_dump($this->m_handle);

        return ($t_result)? (int)$t_result['id']: -1;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:27:04+00:00Added an answer on May 18, 2026 at 8:27 pm

    When you place something in the session and the script ends, PHP goes through each object and calls the __sleep() magic function. Once it resumes the session, __wakeup() is called.

    Resources are not stored in sessions. So every time you want to have a resource available to you, you must initialize it on every script run. This can easily be done, in your example, by implementing the __wakeup() magic method, which should call $this->_init();.

    Your code could then become:

    <?php
    
    include_once 'handler.php';
    
    class DBHandler extends HandlerAbstract { private $m_handle;
    
        function __construct() {
            parent::__construct();
        }
    
        public function test() {
            #TODO: isdir liquibase
            #TODO: isfile liquibase-195/liquibase + .bat + execrights
            $this->m_isTested = true;
        }
    
        public function _init() {
            if (!$this->isTested()) $this->test();
            if (!file_exists('files/data.db')) {
                #TODO: How to to if host is Windows based?
                exec('./files/liquibase-1.9.5/liquibase --driver=org.sqlite.JDBC --changeLogFile=files/data_db.xml --url=jdbc:sqlite:files/data.db update');
                #TODO: quit if not success
            }
    
            #TODO: Set with default data
            try {
                $this->m_handle = new SQLite3('files/data.db');
            } catch (Exception $e) {
                die("<hr />" . $e->getMessage() . "<hr />");
            }
    
            // query works here
            $this->m_isSetup = true;
        }
    
        public function teardown() {
    
        }
    
        public function getUserID($name) {
            //  PHP Warning:  SQLite3::prepare(): The SQLite3 object has not been correctly initialised in
            $t_statement = $this->m_handle->prepare("SELECT id FROM users WHERE name = :name");
            $t_statement->bindValue(":name", $name, SQLITE3_TEXT);
            $t_result = $t_statement->execute();
            //var_dump($this->m_handle);
    
            return ($t_result)? (int)$t_result['id']: -1;
        }
    
        /**
        * Start up the SQLite resource once
        * the session is started.
        * 
        */
        public function __wakeup() {
            $this->init();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

**I've gotten a few suggestions to make my function pure generic, which would work,
First and foremost, are there many android developers here? Is this a good place
I kind of grasp the whole delayed execution concept, but the following has me
I am a junior software engineer who've been given a task to take over
I am familiar with the usage of Bonjour for advertising services on the local
I've added a WCF service reference to a .NET project using this WSDL: https://interop.cmiservices.org/axis/services/CAP1_1?wsdl
We have just found we are getting framing errors (as reported by the WCF
I am trying to make a case to my bosses on why we should
(New to Objective-C, but well versed in C/C++). Presently I have an Objective-C class
I'm using Visual Studio 2010 Express with Framework 4.0 and if I am correct,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.