我有一个wierd php问题。我用下面的代码从mysql获取数据
SELECT kitap.kit_id, kitap.kit_adi, yazar.yad as yazar,
yayinevi.yeviad as yayinevi,kitap.isbn,kitap.basim_yili,
kitap.baski_no,kitap.dil,kitap.cilt,kitap.sayfa,
kitap.kategori,kitap.durum
FROM kitap
INNER JOIN kitapyayinevi ON kitap.kit_id = kitapyayinevi.kit_id
INNER JOIN kitapyazar ON kitap.kit_id = kitapyazar.kit_id
INNER JOIN yayinevi ON kitapyayinevi.yevi_id = yayinevi.yevi_id
INNER JOIN yazar ON kitapyazar.yazar_id = yazar.yazar_id
SQL;
$stmt = $connection->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result); //This is for debugging purposes
但当我试图用下面的代码打印它们时;
<?php foreach ($result as $item): ?>
<tr>
<td class="hide"><?= $item->kit_id; ?></td>
<td><?= $item->kit_adi; ?></td>
<td><?= $item->yazar; ?></td>
<td><?= $item->yayinevi; ?></td>
<td><?= $item->isbn; ?></td>
<td><?= $item->baski_no; ?></td>
<td><?= $item->basim_yili; ?></td>
<td><?= $item->dil; ?></td>
<td><?= $item->cilt; ?></td>
<td><?= $item->sayfa; ?></td>
<td><?= $item->kategori; ?></td>
它给了我下面的错误。除了$item->yazar和$item->yayinevi之外的所有内容都可以打印出来;
注意:未定义的属性:stdclass::$yazar位于c:\users\nihal\documents\kutuphaneyonetim\php\body.php的第38行
注意:未定义的属性:stdclass::$yayinevi在c:\users\nihal\documents\kutuphaneyonetim\php\body.php的第39行
varèu dump($result)这样打印出来;
array(1) { [0]=> array(12) {
["kit_id"]=> string(2) "25"
["kit_adi"]=> string(7) "Sınır"
["yazar"]=> string(13) "Beyza Alkoç "
["yayinevi"]=> string(15) "İNDİGO KİTAP"
["isbn"]=> string(10) "6052361580"
["basim_yili"]=> string(4) "2018"
["baski_no"]=> string(1) "4"
["dil"]=> string(8) "TÜRKÇE"
["cilt"]=> string(12) "Karton Kapak"
["sayfa"]=> string(3) "400"
["kategori"]=> string(5) "Roman"
["durum"]=> string(5) "rafta"
}
}
有什么问题吗?你们能帮我找到吗?
2条答案
按热度按时间bgibtngc1#
你曾经
获取\u assoc,以便它们都作为数组返回。你把它们当作对象来处理,例如
alternativley将fetchall更改为
把代码保持原样。
klsxnrf12#
尝试将obj转换为array,这样如果您的值obj或值为null,它就不会出现任何错误,并且很容易管理array。
在循环之前添加。