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
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();
?>

Link to comment
Share on other sites

sorry about that 

 function showData()
 {
        $q = "select * from user where id=" . $this->userId;
        $r = $this->connectionString();
        $result = $r->query($q);
        while ($row = $result->fetch_assoc()) 
        {
            echo $row['name'];
        }
}

try replacing your showdata() function by above function

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.