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 7684271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:59:17+00:00 2026-05-31T18:59:17+00:00

i have these in Database.php class Database { private $dbname = ‘auth’; private $db_instance;

  • 0

i have these in Database.php

class Database
{
    private $dbname = 'auth';
    private $db_instance;   

    public function __construct($q)
    {
        if(!$this->db_instance)
        $this->db_instance = new SQLite3($q);
    }

    private function init()
    {
        if(!$this->db_instance)
        {
            $this->db_instance = new SQLite3($dbname);
        }
    }

    private function close()
    {
        if($this->db_instance)
        $this->db_instance->close();
    }

    static public function fetch_a_row($q)
    {
        $this->init();
        $res = $this->db_instance->query(SQLite3::escapeString($q));
        $ret = $res->fetchArray(SQLITE3_ASSOC); 
        $this->close();

        return $ret;
    }

    static public function exec($q)
    {
        $this->init();
        $this->db_instance->exec(SQLite3::escapeString($q));
        $this->close();
    }


}

in try to call him on index.php

<?php
require_once('Database.php');

$ret = Database::fetch_a_row('SELECT * FROM user WHERE uname = "test"');

echo $ret['id'];
?>

but response :

Fatal error: Using $this when not in object context in /www/cgi-bin/auth/Database.php on line 29

is there anyone could help ?
thank in advance

  • 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-31T18:59:18+00:00Added an answer on May 31, 2026 at 6:59 pm

    In your static method, you are using the instance accessor $this-> which is not valid syntax. Typically, you would need to use self::, but it isn’t as simple as that. Take a look at this question, it should explain the difference between using self (when in static methods) and $this (when in non-static methods).

    From a glance at the code though, the Database class requires you to instantiate the object in order to create the DB instance utilised later on. For this reason, it would make more sense to make your query methods fetch_a_row and exec non-static, and change your code to instantiate the object and use that instance.

    However, this is all stuff that you must figure out as the Database class needs a bit of refactoring in order to work. The first thing to ask yourself would be “why are you utilising static methods?” – if you don’t need them, it may be simpler to use non-statics (as mentioned above). From there, I guess it’s up to you the direction you take.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP mysql database I have created a follow on question to this one here
I have these images that I am getting off my server and a database
Say I have a database called Poll and have these two table structures. poll
Say I have a database containing Books and Users and these users have certain
Say you have a string 3.4564. A sample set in the database has these
I have a field in my database that contain comma separated values these values
I have a database with Users. Users have Items. These Items can change actively.
I have a database with 150 million products, I would like index these using
I have a Ci script outputting database values. On these values '#commentLink' and '#commentBox'
i have a stack of messages in database table. i want to send these

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.