How to make an INNER JOIN with two tables in different Banks?

Asked

Viewed 584 times

1

I have two different banks that I make the connection as follows:

date_default_timezone_set('America/Sao_Paulo');
abstract class BancoDados
{
const host = 'localhost';
const novoweb = 'novoweb';
const WebAccount = 'Web_Account';
const Member = 'Member';
const user = 'usuario';
const password = 'senha';
static function conectarW()
{
    try 
    {
        $pdoW = new PDO("mysql:host=".self::host.";dbname=".self::novoweb.";charset=utf8", self::user, self::password);
        $pdoW->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdoW;
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}
static function conectarWA()
{
    try 
    {
        $pdoWA = new PDO("mysql:host=".self::host.";dbname=".self::WebAccount.";charset=utf8", self::user, self::password);
        $pdoWA->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdoWA;
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}
}

I have two values in the bank novoweb on the table shopweb with the columns id and nome, where the id I insert in the bank Web_Account in the Gametail table in the column ItemIdx but I don’t know how to do the INNER JOIN because I carry out the consultation so.

static function historicoshopweb($pdoWA,$id,$admin)
{
    try {
        if ($admin == 1) {
            $historico = $pdoWA->prepare("SELECT * FROM GameTail  ORDER BY RegDate DESC");
            $historico->execute();
        } else {
            $historico = $pdoWA->prepare("SELECT * FROM GameTail WHERE IdIdx = :id ORDER BY RegDate DESC");
            $historico->bindValue(":id",$id);
            $historico->execute();
        }
            $historicos = $historico->fetchAll(PDO::FETCH_OBJ);

            return $historicos;

    } catch (PDOException $e) {
        echo "ERROR: ".$e->getMessage();
    }
}

So I want instead of the ItemIdx appear the item name.

1 answer

1


From what I understand, both banks are on the same server and are accessed by the same user. So try to make the connection without passing the database name and do Join like this:

SELECT g1.*, g2.* FROM WebAccount.GameTail g1 left join novoweb.GameTail g2 on g2.IdIdx = g1.IdIdx ORDER BY g1.RegDate DESC
  • I use $pdoW or $pdoWA? it’s not clear to me.

  • Try to make a connection without passing the "dbname" this way: $pdoW = new PDO("mysql:host=".self::host.";charset=utf8", self::user, self::password);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.