dongqinta4174 2019-04-10 22:36
浏览 42
已采纳

如何回显数据库中的特定元素

I'm trying to echo certain elements from my news table throughout an article. This involves fields such as the author, headline, content and date. At the moment I can only echo the row and any attempt to echo elements in the page itself is either met with small errors or it outputs nothing. I've had a look around and only found people having problems with simply printing out the row database which I can already do.

Basically when I have Author in my article I want to echo author from my database and it will show next to Author. I'm not sure how possible it is as I am yet to find anything on this or I'm overlooking it.

Here is my current PHP file and I've left in the initial part with the article author, date and headline.

<?php

/* Database connection settings */
$host = 'xxxxx';
$user = 'xxxxx';
$pass = 'xxxxx';
$db = 'xxxxx';

$dbconnect=mysqli_connect($host,$user,$pass,$db);

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}


 $query = mysqli_query($dbconnect, "SELECT * FROM news WHERE news_Id = 1")
   or die (mysqli_errr($dbconnect));

while ($row = mysqli_fetch_array($query)) {
  echo
   "<tr>
    <td>{$row['headline']}</td>
    <td>{$row['content']}</td>
    <td>{$row['author']}</td>
    <td>{$row['date']}</td>   
   </tr>
";

}

?>
<div class="container-fluid bg">
<div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10">
    <div class="container">         
        <div class="row mb-2">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="news-title">
                                    <h2><?php echo $row['headline']; ?></h2>
                                </div>
                                <div class="news-cats">
                                    <ul class="list-unstyled list-inline mb-1">
                                         <li class="list-inline-item">
                                                <i class="fa fa-folder-o text-danger"></i>
                                                <a href="#"><small>Author:</small> </a>
                                        </li>
                                         <li class="list-inline-item">
                                                <i class="fa fa-folder-o text-danger"></i>
                                                <a href="#"><small>Posted:</small></a>
                                        </li>
                                    </ul>
                                </div>
                                <hr>
  • 写回答

2条回答 默认 最新

  • duanliaogui4133 2019-04-10 23:01
    关注

    Since your query only returns one row, a while loop is inappropriate for fetching the data, as at the end of the loop, $row will be left as a false value, thus making any attempt to access e.g. $row['headline'] fail. Change your while loop

    while ($row = mysqli_fetch_array($query)) {
    

    to a simple assignment:

    $row = mysqli_fetch_array($query) or die(mysqli_error($dbconnect));
    

    Note you have a typo earlier in your code,

    or die (mysqli_errr($dbconnect));
    

    should be

    or die (mysqli_error($dbconnect));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?