我有3张桌子
tbl_news_articles
1 newsIDPrimary int(4)
2 type int(3)
3 title varchar(40)
4 body medium text
5 date date
tbl_images
1 imageIDPrimary int(4)
2 filename varchar(40)
tbl_news_images
1 newsIDIndex int(4)
2 imageIDIndex int(4)
因此,tbl_news_article保存文章数据,tbl_images保存图像数据,而tbl_new_images保存文章ID和图像ID。
每篇新闻文章通常会有1张图片,但可以有多张图片,但是有时某篇新闻文章没有图片。
当文章没有图像时,问题仍然存在。
我的查询是
$sql = "SELECT n.newsID, n.type, n.title, n.body, n.date, ni.newsID, ni.imageID, i.imageID, i.filename FROM tbl_news_articles AS n
LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID
LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
ORDER BY date DESC
返回以下记录
newsID -> 61
type -> 0
title -> title 1
body -> article body text
date -> 2016-10-01
newsID -> NULL
imageID -> NULL
imageID -> NULL
filename -> NULL
newsID -> 62
type -> 0
title -> title 2
body -> article body text
date -> 2016-10-01
newsID -> 62
imageID -> 2
imageID -> 2
filename -> group.jpg
在我的网站上,我显示了每篇文章的摘要,并带有指向全文的链接,问题是,当试图单击数据库中没有链接到其上的图像的文章的全文链接时,newsID行返回NULL而不是实际的newsID值。
我的PHP代码是
<?php
//call the database to retreive the records
$sql = "SELECT n.newsID, n.type, n.title, n.body, n.date, ni.newsID, ni.imageID, i.imageID, i.filename FROM tbl_news_articles AS n
LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID
LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
ORDER BY date DESC LIMIT $offset, $rowsperpage";
$result = $conn->query($sql);
$i = 0;
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$id = $row['newsID'];
$link = "article.php?id=".$id;
$type = $row['type'];
$title = $row['title'];
$urltitle = strtolower($title);
$urltitle = str_replace(" ","-",$urltitle);
$body = $row['body'];
#$bodytext = (strlen($body) > 130) ? substr($body,0,130).'...' : $bodytext;
$pos= strpos($body, ' ', 140);
$bodytext = substr($body,0,$pos);
$bodytext .= "... <a href='$link' title='$title'>read more</a>";
$date = $row['date'];
$formated_date = date("d-M-Y", strtotime($date));
$imgID = $row['imageID'];
$filename = $row['filename'];
if($filename != "")
{
$imgLink = "images/news-articles/".$id."/".$filename;
}
else
{
$imgLink = "images/news-item-placeholder.jpg";
}
$i++;
if($i == 1)
{
echo "<div class='news-spotlight'>";
}
elseif($i >=2 && $i <=3)
{
if($i == "2")
{
$class = "first";
}
else
{
$class = "";
}
echo "<div class='news-highlight $class'>";
}
else
{
echo "<div class='news-item'>";
}?>
<a itemprop="url" href="<?php echo $link?>"><img itemprop="image" src="<?php echo $imgLink?>" alt="<?php echo $title?>" title="<?php echo $title?>"></a>
<div class='data'>
<h3><a href="<?php echo $link?>"><span itemprop="name"><?php echo $title?></span></a></h3>
<div class="article-date"><?php echo $formated_date?></div>
<span itemprop="startDate" content="<?php echo $date?>"></span>
<div itemprop="location" itemscope itemtype="http://schema.org/Place">
<span itemprop="address" content="England"></span>
<span itemprop="name" content="MayoShinDo Association"></span>
</div>
<p itemprop="description"><?php echo $bodytext?></p>
<?php if($i == 1)
{?>
<ul>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 1</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 2</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 3</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 4</a></li>
</ul><?php
}?>
</div><?php
echo "</div>";
}
}
else
{
echo "0 Results";
}?>
我确定问题出在联接上,但我尝试使用内部联接外部联接,但均未返回预期数据。
我什至尝试使用以下
$id = $row['n.newsID'];
以特定的newsID字段为目标,但这进一步打破了局面
谁能帮忙吗?
谢谢
最佳答案
问题在于您同时选择了n.newsID
和ni.newsID
。当有图像链接时,它们将是相同的。但是,当没有图像链接时,n.newsID
包含ID,而ni.newsID
是NULL
。如果选择多个具有相同名称的列,则$row['columnName']
将包含SELECT
列表中的最后一列,因此在这种情况下,它包含ni.newsID
。表名称前缀不包含在关联数组的列名称中。
从ni.newsID
列表中删除SELECT
,或者给它一个别名,例如ni.newsID AS ni_newsID
。然后$row['newsID']
将是n.newsID
,而$row['ni_newsID'] will be
ni.newsID`。