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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真