DelleIsDelle
DelleIsDelle

Reputation: 53

Get related username from mysql table A from same id listed in table B

I'm a newbie and I thought i figured this out before but now i can't get it to work. any help will be greatly appreciated.

Table A lists user ids pulled from table B

table B has detailed information such as username firstname lastname etc linked to the aforementioned user ids

I want to username to display using the referenced userid

I have the function and the the actual call of the function on different pages in the site.

<?php
class Artist {
    private $id;
    private $artistPic;
    private $artistId;
    private $artistPrice;
    private $owner;

    public function __construct($id) {
        global $database;
        $artist = $database->get('artists', '*', ['id' => $id]);

        $this->id = $artist['id'];
        $this->artistPic = $artist['artistPic'];
        $this->artistId = $artist['id'];
        $this->artistPrice = $artist['artistPrice'];
        $this->owner = $artist['owner'];


    }

    static function getLoggedIn() {
        global $database;

        if( isset($_SESSION['userLoggedIn']) ) {
            $artist = $database->get('artists', '*', ['name' => $_SESSION['userLoggedIn']]);

            $classArtist = new self($artist['id']);

            return $classArtist;
        }
    }

    public function getartistPic() {
        return $this->artistPic;
    }

    public function getArtist() {
        return $this;
    }

    public function getOwnerName() {
        global $database;

        $owner = $database->get('artists', 'owner', ['id' => $this->getId()]);
        return $owner;


    }
} ?>

This is when i call the function. but it just shows me the id. all other attempts just goes completely wrong.

<?php echo $artist->getOwnerName($id); ?>

Upvotes: 0

Views: 41

Answers (1)

bhuvnesh pattnaik
bhuvnesh pattnaik

Reputation: 1463

Use This query to fetch Data from Database in PHP and you are all done.

For getting data from only Table B

Select tableB.* From tableA INNER JOIN tableB ON tableA.userID = tableB.userID;

For getting data from only Table A

Select tableA.* From tableA INNER JOIN tableB ON tableA.userID = tableB.userID;

For getting data from both tables

Select * From tableA INNER JOIN tableB ON tableA.userID = tableB.userID;

Upvotes: 1

Related Questions