[英]PHP PDO fetch and foreach - I'm not getting the first column
This is my first post, so I'm just getting to know how the community works so I can be helpful too, later on... 这是我的第一篇文章,因此我将逐渐了解社区的工作方式,以便日后也能为您提供帮助。
I have an index.php and db.php for testing PDO in a very small and simple app, in the first one goes the html and the second the database connection. 我有一个index.php和db.php用于在一个非常小和简单的应用程序中测试PDO,第一个是html,第二个是数据库连接。
index.php index.php
<h1><?php echo $fjoin_bookname; ?></h1>
<?php foreach($join as $j): ?>
<tr>
<td>
<?php echo 'Page: '. $j['pageNumber']; ?>
</td>
<td>
<?php echo $j['pageNote']; ?>
</td>
</tr>
<?php endforeach; ?>
db.php db.php
//get books and pages Join
$query='SELECT *
FROM books
INNER JOIN pages
ON books.bookID = pages.bookID
ORDER BY pageNumber ASC';
$join = $db->query($query);
$fjoin = $join->fetch();
$fjoin_bookname = $fjoin['bookName'];
It is an app that gets page numbers with a corresponding note from a book, this will help to keep track of several books while on different devices. 这是一个从书中获取带有相应注释的页码的应用,这将有助于在不同设备上跟踪几本书。
Problem: I'm not getting the first row from the 'pages' table. 问题:我没有从“页面”表中获得第一行。 It was working fine until I inserted the fetch method
在插入fetch方法之前,一切正常
$fjoin = $join->fetch();
$fjoin_bookname = $fjoin['bookName'];
Question Would anyone be so kind to help me work this out? 问题有人会帮助我解决这个问题吗?
When you do a call of 当您打电话给
$join->fetch()
it returns item from current position and move cursor to the next one. 它从当前位置返回项目并将光标移至下一个。 You can fetch all elements to a var and then get fjoin_bookname from the first element:
您可以将所有元素都提取到var中,然后从第一个元素中获取fjoin_bookname:
db.php db.php
//get books and pages Join
$query='SELECT *
FROM books
INNER JOIN pages
ON books.bookID = pages.bookID
ORDER BY pageNumber ASC';
$statement = $db->query($query);
$join = $statement->fetchAll();
$fjoin = $join[0];
$fjoin_bookname = $fjoin['bookName'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.