我有三张table
tbl\新闻\文章
1 newsIDPrimary int(4)
2 type int(3)
3 title varchar(40)
4 body medium text
5 date date
tbl\U图像
1 imageIDPrimary int(4)
2 filename varchar(40)
tbl\新闻\图片
1 newsIDIndex int(4)
2 imageIDIndex int(4)
所以tbl\u news\u article保存文章数据,tbl\u images保存图像数据,tbl\u new\u images保存文章的id和图像的id。
每个新闻文章通常会有一个图像,但可以有多个图像,但有时没有一个特定的新闻文章的图像。
当一篇文章没有图片时,问题就一直存在。
我的问题是
$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字段为目标,但这又进一步打破了局面,有人能帮忙吗?
谢谢
1条答案
按热度按时间yeotifhr1#
问题是你选择了两者
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`。