将mysql变量输出到php变量

cngwdvgl  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(298)

如何输出php中“as variable”函数设置的mysql变量?

function ClassBalance()
{
    global $con_char;

    $data = $con_char->prepare('SELECT 
        (SELECT class FROM characters WHERE class = 1) AS warrior,
        (SELECT class FROM characters WHERE class = 2) AS paladin,
        (SELECT class FROM characters WHERE class = 3) AS hunter,
        (SELECT class FROM characters WHERE class = 4) AS rogue,
        (SELECT class FROM characters WHERE class = 5) AS priest,
        (SELECT class FROM characters WHERE class = 7) AS shaman,
        (SELECT class FROM characters WHERE class = 8) AS mage,
        (SELECT class FROM characters WHERE class = 9) AS warlock,
        (SELECT class FROM characters WHERE class = 11) AS druid');
    $data->execute();

    while($result = $data->fetchAll(PDO::FETCH_ASSOC))
    {
        foreach($result as $row)
        {
            echo $row['warrior'];
        }
    }
}

ClassBalance();

我不能让它以任何方式工作,它既不输出任何错误,所以我不知道如何继续这个。
感谢在这个问题上我能得到的所有帮助。

5q4ezhmt

5q4ezhmt1#

[测试此代码][1]

function ClassBalance()
{
    global $con_char;

    $data = $con_char->prepare('SELECT 
        (SELECT class FROM characters WHERE class = 1) AS warrior,
        (SELECT class FROM characters WHERE class = 2) AS paladin,
        (SELECT class FROM characters WHERE class = 3) AS hunter,
        (SELECT class FROM characters WHERE class = 4) AS rogue,
        (SELECT class FROM characters WHERE class = 5) AS priest,
        (SELECT class FROM characters WHERE class = 7) AS shaman,
        (SELECT class FROM characters WHERE class = 8) AS mage,
        (SELECT class FROM characters WHERE class = 9) AS warlock,
        (SELECT class FROM characters WHERE class = 11) AS druid');
    $data->execute();
    $store_array=array();
    while($result = $data->fetchAll(PDO::FETCH_ASSOC))
    {
        foreach($result as $row)
        {
           array_push($store_array,$row);
        }
    }    
    return $store_array;
}

ClassBalance();

相关问题