无法访问其他表数据SQL

时间:2018-07-07 08:54:09

标签: php mysql sql

我有两个相互关联的表(如屏幕截图所示):

https://picr.eu/i/wmAT

现在,我从表格zerts中进行选择,并希望将所有zerts放在列表中。现在,我想通过用户标识获取用户名。如何访问已连接的数据?

$sql = "SELECT * FROM zerts ORDER BY created";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($result)) {
      echo "<tr><td>" . $row["id"] . "</td><td>" . $row["userid"] . "</td><td>" . $row["username"] . "</td></tr>";
    }

为什么无法通过用户ID获取用户名?我在用户名字段中得到空白输出。

2 个答案:

答案 0 :(得分:0)

您需要为此创建联接才能访问其他表的用户名。

#include <iostream>
#include "Piso1.h"

using namespace std;
Piso1::Piso1(){

};

int Piso1::Draw(RenderWindow &window, Event &evento)
{
srand(time(nullptr));
Soundtrack.openFromFile("../Scenes/Piso1/Sounds/Neon District.wav");
    Soundtrack.setLoop(true);
    Soundtrack.play();

Texture BGTexture;
    BGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1background.png");
Sprite Background;
    Background.setTexture(BGTexture);
    Background.setScale(8,7.5);
    Background.setPosition(BackX,BackY);


Texture FGTexture;
    FGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1foreground.png");
Sprite Foreground;
    Foreground.setTexture(FGTexture);
    Foreground.setScale(8,7.5);
    Foreground.setPosition(BackX,BackY);

Texture ProtaTextura;
    ProtaTextura.loadFromFile("../Scenes/Piso1/Graphics/pSprite.png");
    IntRect SpriteBx(0,0,34,47);
Sprite Protagonista(ProtaTextura,SpriteBx);
    Protagonista.setPosition((window.getSize().x)/2.35,(window.getSize().y)/3);
    Protagonista.setScale(3,3);

while (window.isOpen()) {
    while (window.pollEvent(evento)) {
        switch (evento.type) {
            case Event::Closed:
                window.close();
                break;
            case Event::KeyPressed:
                EncounterValue = rand()%1000;
                if(EncounterValue > 5){
                    if(evento.key.code == Keyboard::Down) {
                        BackY -= 10;
                        Background.move(0,-10);
                        Foreground.move(0,-10);
                        //this is my failed attempt
                        if(Protagonista.getLocalBounds().intersects(Foreground.getLocalBounds()))
                        {
                            Collision.openFromFile("../Scenes/Piso1/Sounds/oof.ogg");
                            Collision.play();
                            BackY += 10;
                            Background.move(0, 10);
                            Foreground.move(0, 10);
                        }

                        if(clock1.getElapsedTime().asMilliseconds()>64){
                            SpriteBx.top = 0;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if (evento.key.code == Keyboard::Up) {
                        BackY += 10;
                        Background.move(0,10);
                        Foreground.move(0,10);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 152;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::Left) {
                        BackX += 10;
                        Background.move(10,0);
                        Foreground.move(10,0);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 53;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::Right){
                        BackX -= 10;
                        Background.move(-10,0);
                        Foreground.move(-10,0);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 104;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::C){
                        Soundtrack.stop();
                        return 1;
                    }
                }
                else{
                    Soundtrack.stop();
                    return 0;
                }
        }
        window.clear();
        window.draw(Foreground);
        window.draw(Background);
        window.draw(Protagonista);
        window.display();
    }
}
}

答案 1 :(得分:0)

请更新您的查询,其余代码相同。

$sql = "SELECT zerts.*,users.username FROM zerts 
LEFT JOIN users ON (zerts.userid = users.id)  ORDER BY zerts.created";

希望获得帮助。