douquanzhan0315 2017-11-02 09:42
浏览 21

未捕获的错误:使用$ this

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

  • 写回答

2条回答 默认 最新

  • duanken7168 2017-11-02 09:52
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥15 怎么实现输入一个要删除的数后删除后显示剩余数再输入再删除显示剩余数(语言-c语言)
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection