BR1888
BR1888

Reputation: 43

How to echo specific elements from a database

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>\n";

}

?>
<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>

Upvotes: 0

Views: 499

Answers (2)

Nick
Nick

Reputation: 147206

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));

Upvotes: 1

Yoko Ishioka
Yoko Ishioka

Reputation: 161

You are just printing out the result, but don't have a label. You can add one like this:

while ($row = mysqli_fetch_array($query)) {
  echo
   "<tr>
    <td>Headline: {$row['headline']}</td>
    <td>Content: {$row['content']}</td>
    <td>Author: {$row['author']}</td>
    <td>Date: {$row['date']}</td>   
   </tr>\n";

}

Upvotes: 1

Related Questions