【发布时间】:2016-04-23 09:06:11
【问题描述】:
我有一个新闻项目表(包含字段标题、内容、日期、来源和主索引)。使用$newsrow['title'],我成功地获取了第一行项目的标题,但此后无法访问下一行的项目。我尝试了$newsrow[#]['title'](我的意思是 $newsrow[0]['title'] 来访问第一行的标题),但是那个标志:Warning: Illegal string offset。会避免在这里使用循环,因为里面有一些异常,我认为这里可能有更直接的方法。
下面是我调用的函数(newfunction.php):
<?php
function news_data($conn)
{
$query = "SELECT * FROM news ORDER by newstrack ASC LIMIT 3";
$result = $conn->query($query);
$row = $result -> fetch_array(MYSQLI_BOTH);
return $row;
}
?>
这是我在前端访问值的方式:
<div class="art1">
<h4 class="newshead"><?php echo $newsrow[0]['title'];?></h4><br>
<img src="images/jaipur2.png" class="pic1">
<p class="newspara">Sessions unfolded at a charming colonial heritage property. Among the guests were ...</p><br>
</div>
<img class="tab3" src="images/newstab.png" alt="main">
<a href="reviews.html">news</a>
<div class="art2">
<h4 class="newshead"><?php echo $newsrow['title'];?></h4><br>
<img src="images/aksh.png" class="pic2">
<p class="newspara">From a trip to the temple to a meeting with students </p>
</div>
<div class="art3"><br>
<h4 class="newshead">FEST A HIT WITH VISITORS</h4><br>
<img src="images/kumaon.png" class="pic3">
<p class="newspara">The first edition of the two-day Food Festival kicked off <b>...</b></p>
</div>
</section>
我的设置建立连接并将上述函数传递给数组变量:
$newsrow = news_data($conn);
希望了解如何在不循环的情况下访问特定行的值。
【问题讨论】:
-
帮我一个忙,请
var_dump($row);。 -
它只抛出第一行的细节:
array(16) { [0]=> string(1) "1" ["newstrack"]=> string(1) "1" [1]=> string(44) ...