0

I've seen other posts about this but I just can't understand them, what is the solution here, can you guys show me? Please. Static or not static? What does it mean? Is this the problem?

Db connection code:-

<?php 
define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'root'); 
define('DB_PASSWORD', ''); 
define('DB_DATABASE', 'not telling'); 
class DB_con { 
    public $connection; 
    function __construct(){ 
        $this->connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD,DB_DATABASE); 
        if ($this->connection->connect_error) 
            die('Database error -> ' . $this->connection->connect_error); 
    } 
    function ret_obj(){ 
        return $this->connection; 
    } 
}

Code for fetching records:-

<?php

$role_id = (isset($_SESSION['role_id']) ? $_SESSION['role_id'] : 4) ;

$query = "
   SELECT rights_codename FROM permissions 
   INNER JOIN roles_and_permissions ON fk_permissions_id = permissions_id
   WHERE fk_role_id = $role_id
   ";

$_SESSION['permissions'] = array();
$result = $this->db->query($query);
while($row = $result->fetch_assoc()){

   array_push($_SESSION['permissions'], $row['permissions_cname']);

 //  $_SESSION['permissions'][] = $row['permissions_cname'];

}
if(in_array('admin_rediger_bruger',$_SESSION['permissions'])){
}

?>

Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\R_L_E\login.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\R_L_E\login.php on line 30

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

2 Answers2

1

In lay man terms, $this is when you are referencing a non static function in a class. What you can do is create a new instance of the database class and then use that variable created during instantiation to access the member functions of that class

Do this

$conn = new DB_con();//instantiate the class.add this at the very top of login.php
$conn->connection->query('your sql stuff');//replace the $this->db->query($query) with this line 

You can access the connection property as it is declared as public in your class

Also do not forget to include the DB_con file

Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

$this is a variable that refers to "the current object" when you're running code inside that object. That means, when you write this:

class AClass {
    public function foo() {
        var_dump($this);
    }
}
$my_object = new AClass;
$my_object->foo();

Then $this is the same as $my_object during the execution of the foo() function. You can think of it like an extra parameter to the function, once you leave that function it won't exist any more.

So when you write this:

$result = $this->db->query($query);

You need to be inside some method which has been called on a particular object, so that there is an object for $this to refer to. If you're in global code, or a function that's not part of a class, or a static method, there is no "current object", so $this doesn't exist.

In your case, you're trying to get to some instance of a DB connection object. That means somewhere in your code you need to have created that object, and assigned it to a variable, then you can reference that variable. You can call that variable whatever you like, but you can't call it $this, because that's a reserved name.

Your DB_Con class doesn't have a query() method, so it looks like you want to get the MySQLi object out first, and then call the method on that.

So you would write something like:

 $my_db_connection = new DB_Con;
 $mysqli_connection = $my_db_connection->connection;
 // or: $mysqli_connection = $my_db_connection->ret_obj();
 $result = $mysqli_connection->query($query);

Or more concisely:

 $my_db_connection = new DB_Con;
 $result = $my_db_connection->connection->query($query);
 // or: $result = $my_db_connection->ret_obj()->query($query);
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • https://postimg.org/gallery/39s2qs54c/ i change: $this->db = new DB_con(); out with $my_db_connection = new DB_Con; and $result = $this->db->query($query); out with $result = $my_db_connection->connection->query($query);? the first one is in class.user file with a link to database the second line is in login with a link to class.user file – Deniz Bircan Nov 02 '17 at 10:16
  • @DenizBircan Sorry, I don't have time to write your code for you. You'll need to try things out and see if they work, and try not to copy and paste examples without understanding *why* the code looks that way. Start simple, understand the principles, and work your way up. – IMSoP Nov 02 '17 at 10:22