将MySQL变量输出到PHP变量

时间:2018-01-03 03:16:39

标签: php mysql variables

如何在PHP中输出由“AS变量”函数设置的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();

我不能让它以任何方式工作,它既没有输出任何错误,所以我不知道如何继续这个。

感谢我对此主题的所有帮助。

1 个答案:

答案 0 :(得分:-1)

[测试此代码] [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();