lundi 23 août 2021

How to declare object from class to access method in PHP

I try to declare object from class "anyclass.php" but it doesn't work. how can i resolve it

anyclass.php

class AnyClass {

private function Database_connection()
{
    $this->servername = "localhost";
    $this->username = "root";
    $this->password = "SJ5cohmzXiFquSBJ";
    $this->dbname = "pharmacy";
    $this->conn = new mysqli(  $this->servername,   $this->username,   $this->password,   $this->dbname) or die("Error Connect..");
    $this->conn->set_charset("utf8");
    $this->conn->query("SET NAMES UTF-8");
    return $this->conn;
}
public function querysql($sql){
    $connect = $this->Database_connection();
    // $sql = "SELECT * FROM `hospital`";
    $this->result = $connect->query($sql);
    return $this->result;
}

}

testclass.php

include 'anyclass.php';
$ojb = new AnyClass();
$txt = "SELECT * FROM `hospital`";
$result = $obj->querysql($txt);
if($result){
   echo"YES";
}else echo "NO";

Error is Warning: Undefined variable $obj in C:\xampp\htdocs\test2\testclass.php on line 5

Fatal error: Uncaught Error: Call to a member function querysql() on null in C:\xampp\htdocs\test2\testclass.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test2\testclass.php on line 5




Aucun commentaire:

Enregistrer un commentaire