gpt4 book ai didi

PHP - 不显示数据库中的所有行

转载 作者:行者123 更新时间:2023-11-29 19:32:38 28 4
gpt4 key购买 nike

我有以下代码,应该从数据库中获取多条记录。当查询在 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)

最佳答案

尝试这样的事情:

$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);
});

// ...

关于PHP - 不显示数据库中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41702545/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com