PHP - 未显示数据库

时间:2017-01-17 16:45:12

标签: php mysql

我有以下代码,它应该从数据库中获取几条记录。当在phpmyadmin中运行查询时,它显示行很好,但在页面上它只显示第一个项

我已经回复了$ cardList数组并显示了正确的ID

$cards = array($get['card1'], $get['card2'], $get['card3'], $get['card4'], $get['card5'], $get['card6'], $get['card7'], $get['card8'], $get['card9'], $get['card10']);

foreach($cards as $card) {
    if($card != "0")
        $cardList[] = $card;
}

$sql = "SELECT mc.id, d.deckName, c.cardNumber
    FROM me_member_cards AS mc
    LEFT JOIN me_cards AS c ON c.id = mc.cardID
    LEFT JOIN me_decks AS d ON d.deckName = c.deckName
    WHERE mc.id in (:cardsList)";

$q = $pdo->prepare($sql);
$q->bindValue(':cardsList', implode(',' , $cardList));
$q->execute();
if($q->errorCode() != 0) {
     $errors = $q->errorInfo();
         echo($errors[2]);
}
foreach ($q->fetchAll() as $row) {
    echo $row['id'] . " - " . $row['deckName'] . " " . $row['cardNumber'] . "<br/>";
}

回显查询给出

SELECT mc.id, d.deckName, c.cardNumber FROM me_member_cards AS mc LEFT JOIN me_cards AS c ON c.id = mc.cardID LEFT JOIN me_decks AS d ON d.deckName = c.deckName WHERE mc.id in (29,64,53,13,38,13,8)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$cards = array($get['card1'], $get['card2'], $get['card3'], $get['card4'], $get['card5'], $get['card6'], $get['card7'], $get['card8'], $get['card9'], $get['card10']);

$cardList = $placeholderList = [];
foreach ($cards as $key => $card) {
    if ($card != "0") {
        $cardList[':mc_id' . $key] = $card;
    }
}

$sql = "SELECT mc.id, d.deckName, c.cardNumber
    FROM me_member_cards AS mc
    LEFT JOIN me_cards AS c ON c.id = mc.cardID
    LEFT JOIN me_decks AS d ON d.deckName = c.deckName
    WHERE mc.id in (" . implode(',', array_keys($cardList)) . ")";

$q = $pdo->prepare($sql);

array_walk($cardList, function ($value, $placeholder) use ($q) {
    $q->bindValue($placeholder, $value, \PDO::PARAM_INT);
});

// ...