我在2個表運行查詢,以從博客返回信息。嵌套查詢選擇與該博客相關聯的標籤,該博客是一個單獨的表。PHP選擇字段從嵌套查詢
我希望能夠從「標籤」表獲得「標籤」行和在頁面上顯示這些。我的代碼在下面,我已經評論了我想要選擇的行的位置。
<?php
include("inc/dbconnection.php");
$id = $_GET['tag'];
$id = trim($id);
$result = mysqli_query($conn, "
SELECT *
FROM blog
WHERE blog_id IN (SELECT blog_id FROM tags WHERE tag = '$id')
");
if (!$result) {
die("Database query failed: " . mysqli_error($conn));
} //!$result
else {
$rows = mysqli_num_rows($result);
if ($rows > 0) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$content = $row['content'];
$content2 = substr($content, 0, 100);
/* Below is where I would like to pull the row 'tag' from the nested query */
$rawTag = $row['tag'];
$tag = str_replace(" ", "", $rawTag);
$tagArray = explode(",", $tag);
$tag = "";
for ($i = 0; $i < count($tagArray); $i++) {
$tag .= "<a href='tag-" . $tagArray[$i] . ".php'>" . $tagArray[$i] . "</a> ";
} //$i = 0; $i < count($tagArray); $i++
echo "
<table>
<tr>
<th>
<a href='blog-" . $row['blog_id'] . ".php'>
<h2>" . $row['title'] . "</h2>
</a>
</th>
</tr>
<tr>
<td>
<p>" . date("d/m/Y", strtotime($row['createdDate'])) . "</p><br />
<span>" . $content2 . "...</span><br />
<span><small>Tags: " . $tag . "</small></span>
</td>
</tr>
</table>";
} //$row = mysqli_fetch_array($result, MYSQLi_ASSOC)
} //$rows > 0
else { //$rows > 0
echo "<br /><h1>There are currently no blogs with the selected tag (" . $_GET['tag'] . ")</h1><br /><h2>Please check back later.</h2>";
}
}
?>
很抱歉,如果這是一個愚蠢的問題,並在此先感謝您的幫助:)
好,你正在使用先進的API,但現在看到準備好的發言 – Strawberry 2014-11-23 11:12:16
唉唉拍!對不起,忘了提,-_-我想我的數據庫中顯示從輔助表「標籤」行......我會更新的問題:) – Timothy 2014-11-23 11:16:26
正確的,我只使用1 DB,但有2個表現在在這裏 – Timothy 2014-11-23 11:20:18