繁体   English   中英

从表B中列出的相同ID从mysql表A获取相关的用户名

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

我是一个新手,我想我以前就已经弄清楚了,但是现在我无法正常工作。 任何帮助将不胜感激。

表A列出了从表B中提取的用户ID

表B具有链接到上述用户ID的详细信息,例如用户名,姓氏等

我想使用参考的用户名显示用户名

我在站点的不同页面上具有该函数以及该函数的实际调用。

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


    }
} ?>

这是我调用函数的时间。 但这只是向我显示ID。 其他所有尝试都完全错误。

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

使用此查询从PHP中的数据库中获取数据,您已经完成了。

仅从表B获取数据

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

仅从表A获取数据

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

从两个表中获取数据

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM