Jump to content

access from another class


schubertgergo

Recommended Posts

hi, here is my code:

 

class DBCon{
    private $con;
    private $host;    
    private $user;
    private $password;
    private $dataBase;
    
    function __construct(){
        $this->host     = 'localhost';
        $this->user     = 'root';
        $this->password = '';
        $this->dataBase = 'import';
        $this->con      = new mysqli($this->host, $this->user,$this->password, $this->dataBase);

    }
    
    function connectionString(){        
        if($this->con->error){
            echo "Sikertelen adatbázis kapcsolódás";
            die();
        }
        else{
            return $this->con;
        }
    }
}
 

 

class showUser{

   

    private userId;

    function __construct($id){

        $this->userId=$id;  

    }

 

    function showData(){

        $q="select * from user where id=".$this->id;

        $r=$con->query($q);

        while($row=$r->fetch_assoc()){

            echo $row['name'];

        }

    }

 

}

 

I dont know how to  add the $con value to showuser class, and make the showdata() function work.

 

Thank you for your help.

Link to comment
https://forums.phpfreaks.com/topic/280740-access-from-another-class/
Share on other sites

thank you, but i still have a problem with the query.

 

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\import_\dbconn.php on line 50

 

here is the code:

 

<?php
class DBCon
{
    private $con;
    private $host;
    private $user;
    private $password;
    private $dataBase;

    function __construct()
    {
        $this->host = 'localhost';
        $this->user = 'root';
        $this->password = '';
        $this->dataBase = 'importtable';


    }

    function connectionString()
    {
        $this->con = new mysqli($this->host, $this->user, $this->password, $this->dataBase);
        if ($this->con->error) {
            echo "Sikertelen adatbázis kapcsolódás";
            die();
        } else {
            return $this->con;
        }
    }
}


class showUser extends DBCon
{
    private $userId;

    function __construct($id)
    {
        $this->userId = $id;
    }


    function showData()
    {

        $q = "select * from user where id=" . $this->userId;

        $r = $this->connectionString()->query($q);

        while ($row = $r->fetch_assoc()) {

            echo $row['name'];

        }

    }
}

$o=new showUser(1);
$o->showData();
?>

Sorry not working... i changed:

<?php
class DataBase
{
    private $con;
    private $host;
    private $user;
    private $password;
    private $dataBase;
    private $queryString;
    private $result;
    private $row;

    function __construct()
    {
        $this->host = 'localhost';
        $this->user = 'root';
        $this->password = '';
        $this->dataBase = 'importtable';

        $this->con = new mysqli($this->host, $this->user, $this->password, $this->dataBase);
    }

    function connectionString()
    {
        if ($this->con->error) {
            echo "Sikertelen adatbázis kapcsolódás";
            die();
        }
    }

    function selectUser($query){
        $this->queryString=mysqli_real_escape_string($this->con,$query);
        $this->result=$this->con->query($this->queryString);
        $this->row=$this->result->fetch_assoc();
         echo $this->row['id'];

    }

}


class showUser extends DataBase
{
    private $userId;

    function __construct($id)
    {
        $this->userId = $id;
    }


    function showData()
    {
        if(filter_var($this->userId)){
            $q = "select * from user where id=" . $this->userId;
            $this->selectUser($q);

        }
        else{

        }

    }
}

$o= new showUser(1);
$o->showData();
?>

 

now i have this messages:

 

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\import_\dbconn.php on line 32

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\import_\dbconn.php on line 33

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.