显示多个字符串列表中的一个字符串

时间:2014-02-04 01:29:12

标签: php arrays pdo

我正在尝试从这个数组中获取数据,但当我var_dump($ArrayedLevels['Attack']);它返回NULL时,我知道它能够从SQL数据库中获取数据,我认为它与我的阵列有关。任何帮助将不胜感激。

include("highscoresconfig.php");

$GrabXP = $database2->prepare("SELECT * FROM `skills` WHERE `playerName` = ?");
$GrabXP->execute(array($playerName));
$MainResult = $GrabXP->fetchAll();

$ArrayedLevels = $array = [
    "Attack" => $MainResult['Attacklvl'],
    "Defence" => $MainResult['Defencelvl'],
    "Strength" => $MainResult['Strengthlvl'],
    "Hitpoints" => $MainResult['Hitpointslvl'],
    "Ranged" => $MainResult['Rangelvl'],
    "Prayer" => $MainResult['Prayerlvl'],
    "Magic" => $MainResult['Magiclvl'],
    "Cooking" => $MainResult['Cookinglvl'],
    "Woodcutting" => $MainResult['Woodcuttinglvl'],
    "Fletching" => $MainResult['Fletchinglvl'],
    "Fishing" => $MainResult['Fishinglvl'],
    "Firemaking" => $MainResult['Firemakinglvl'],
    "Crafting" => $MainResult['Craftinglvl'],
    "Smithing" => $MainResult['Smithinglvl'],
    "Mining" => $MainResult['Mininglvl'],
    "Herblore" => $MainResult['Herblorelvl'],
    "Agility" => $MainResult['Agilitylvl'],
    "Thieving" => $MainResult['Thievinglvl'],
    "Slayer" => $MainResult['Slayerlvl'],
    "Farming" => $MainResult['Farminglvl'],
    "Runecrafting" => $MainResult['Runecraftlvl'],
    "Hunter" => $MainResult['Hunterlvl'],
    "Construction" => $MainResult['Constructionlvl'],
    "Summoning" => $MainResult['Summoninglvl'],
    "Dungeoneering" => $MainResult['Dungeoneeringlvl'],
];
var_dump($ArrayedLevels["Attack"]);

4 个答案:

答案 0 :(得分:0)

问题可能是因为fetchAll()返回查询的所有行。

if($GrabXP->execute(array($playerName))){
    //Success
    $MainResult = $GrabXP->fetchAll();
    /*
    This will give you all the rows. Use a foreach loop to iterate through all the rows.
    If you want only the first row, add this-
    */
    $MainResult = $MainResult[0];
    //The rest of your code.
    $ArrayedLevels = $array = Array(
    "Attack" => $MainResult['Attacklvl'],
        ...
    );
    var_dump($ArrayedLevels["Attack"]);
}
else{
    //Failure
}

答案 1 :(得分:0)

创建新数组时,括号在PHP中不起作用,请使用array()创建一个。

实际上:从PHP 5.4开始,它们确实有效,请参见此处:http://nl3.php.net/manual/en/language.types.array.php

答案 2 :(得分:0)

尝试var_dump($MainResult);

如果一切顺利,请尝试创建这样的数组:

$ArrayedLevels = array(
    "Attack" => $MainResult['Attacklvl'],
    "Defence" => $MainResult['Defencelvl'],
    "Strength" => $MainResult['Strengthlvl'],
    "Hitpoints" => $MainResult['Hitpointslvl'],
    "Ranged" => $MainResult['Rangelvl'],
    "Prayer" => $MainResult['Prayerlvl'],
    "Magic" => $MainResult['Magiclvl'],
    "Cooking" => $MainResult['Cookinglvl'],
    "Woodcutting" => $MainResult['Woodcuttinglvl'],
    "Fletching" => $MainResult['Fletchinglvl'],
    "Fishing" => $MainResult['Fishinglvl'],
    "Firemaking" => $MainResult['Firemakinglvl'],
    "Crafting" => $MainResult['Craftinglvl'],
    "Smithing" => $MainResult['Smithinglvl'],
    "Mining" => $MainResult['Mininglvl'],
    "Herblore" => $MainResult['Herblorelvl'],
    "Agility" => $MainResult['Agilitylvl'],
    "Thieving" => $MainResult['Thievinglvl'],
    "Slayer" => $MainResult['Slayerlvl'],
    "Farming" => $MainResult['Farminglvl'],
    "Runecrafting" => $MainResult['Runecraftlvl'],
    "Hunter" => $MainResult['Hunterlvl'],
    "Construction" => $MainResult['Constructionlvl'],
    "Summoning" => $MainResult['Summoninglvl'],
    "Dungeoneering" => $MainResult['Dungeoneeringlvl'],
);

答案 3 :(得分:0)

首先尝试打印变量$ MainResult的结构:

var_dump($MainResult);

FetchAll返回一个包含所有restuls的数组,如

resutl = [
 [0] => ['first row'],
 [1] => ['sec. row']
]

您将看到变量$MainResult看起来像:

MailResult => [
  [0] => ['Attacklvl' => 'foo'],
  [1] => ['Defencelvl' => 'bar'] 
]