Mysqli UNION只返回主键 [英] Mysqli UNION returning only primary key

查看:70
本文介绍了Mysqli UNION只返回主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与使用mysqli联合从两个表中获取数据有关。问题是仅从主键/查询中获取数据,而忽略第二个语句。如果我切换两个语句的位置,新的主查询返回结果,第二个返回null。

我想选择五列;一个公共列和两个表中的两个不同的行然后将它们挖成一个数组。我的意思是下面的例子





My question has to do with fetching data from two tables using mysqli unions. The problem is that data is only fetched from the primary key/query and the second statement is ignored. If I switch the places of both statements, the new primary query returns results and the 2nd one returns null.
I want to select five columns; one common column and two different rows from both tables then scoop them into an array. An example of what I mean is below


static function fetch_me() {
    $conn = new mysqli("st", "root",  "\57", "_db");
    $sess = $_SESSION['username'];

    $sql = "SELECT username, NULL AS profile_pic, NULL AS bio, email, password
            FROM users
            WHERE username='$sess'
            UNION ALL
            SELECT username, profile_pic, bio, NULL AS email, NULL AS password
            FROM user_info
            WHERE username='$sess'";

    if ($x = $conn->query($sql)) {
        return $x->fetch_assoc();
    }
}





我尝试了什么:



我尝试使用它而不使用'ALL`条款加上一些其他实验,但遗憾的是无法敲击黄金。我在网上看到的所有例子都与mysql有关,没有显示一个明确的php示例。上面的查询在phpmyadmin上正常运行,当我从该查询请求PHP代码时,它返回一个仅返回主查询的sql



What I have tried:

I've tried using it without the `ALL` clause plus a few other experimentations but unfortunately unable to strike gold yet. All the examples I'm seeing online are mysql related, none shows an explicit php example. The above query runs fine on phpmyadmin and when I request the PHP code from that query, it returns a sql that only returns the primary query as well

推荐答案

conn = new mysqli( st root < span class =code-string> \ 57, _ db) ;
conn = new mysqli("st", "root", "\57", "_db");


sess =
sess =


_SESSION [' username'];

_SESSION['username'];


这篇关于Mysqli UNION只返回主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆