-1

i'm getting this error and i dont know what to do?

Fatal error: Uncaught Error: Using $this when not in object context in /home/lumato/domains/lumato.nglyceum.eu/public_html/DBController.php:18 Stack trace: #0 /home/lumato/domains/lumato.nglyceum.eu/public_html/drinks.php(144): runQuery('SELECT * FROM P...') #1 {main} thrown in /home/lumato/domains/lumato.nglyceum.eu/public_html/DBController.php on line 18

<?php
function __construct() {
    $this->conn = $this->connectDB();
}

function connectDB() {
    $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
    return $conn;
}

function runQuery($query) {
    $result = mysqli_query($this->conn,$query);
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
}

function numRows($query) {
    $result  = mysqli_query($this->conn,$query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;   
}

?>

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46

1 Answers1

0

You need to learn about Class in PHP.I modified your code for an example

class MyQuery{
    function __construct() {
        $this->conn = $this->connectDB();
    }
    
    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }
    
    function runQuery($query) {
        $result = mysqli_query($this->conn,$query);
        $resultset = [];
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }
    
    function numRows($query) {
        $result  = mysqli_query($this->conn,$query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
}

$myQuery = new MyQuery();
$myQuery->runQuery("select * from ....");

here is php class document

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
nay
  • 1,725
  • 1
  • 11
  • 11